3214 Posts in 848 Topics by 810 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1708 Views |
-
[Solved] JavaScript Watermark on Silver Stripe Search Textbox

16 July 2010 at 4:58am Last edited: 18 July 2010 5:06am
I am trying to implement a JQuery watermark on the textbox dynamically created by Silver Stripe $SearchForm. Without any success.
The search form is located in the header of the Page.ss file located in my themes folder.
Here is my code:
Page.php
----------------------
class Page_Controller extends ContentController {
public static $allowed_actions = array (
);public function init() {
parent::init();
// Stylesheets
Requirements::themedCSS('default');
if($pos = strpos( $_SERVER[ 'HTTP_USER_AGENT' ], 'MSIE' ) ) {
Requirements::css("themes/dea/css/ie.css");
}
// JavaScript
Requirements::javascript('mysite/javascript/jquery-1.4.2.min.js');
Requirements::javascript('mysite/javascript/jquery.watermarkinput.js');
Requirements::javascript('mysite/javascript/dea.js');
}
}dea.js
---------------------
$(document).ready(function(){
$("#SearchForm_SearchForm_Search").Watermark("Search the DEA");
}); -
Re: [Solved] JavaScript Watermark on Silver Stripe Search Textbox

16 July 2010 at 9:55am
Do you get any javascript errors to your browser? Are the watermarkinput.js and dea.js files being loaded correctly on the front end (not 404ing or anything).
-
Re: [Solved] JavaScript Watermark on Silver Stripe Search Textbox

16 July 2010 at 1:51pm
I am receiving this error: $("#SearchForm_SearchForm_Search").Watermark is not a function
Line 2
-
Re: [Solved] JavaScript Watermark on Silver Stripe Search Textbox

16 July 2010 at 2:56pm
Both files are being loaded. This is so weird.
-
Re: [Solved] JavaScript Watermark on Silver Stripe Search Textbox

17 July 2010 at 2:52am
I got it to work.
For whatever reason changing my dea.js file to:
;(function($) {
$(document).ready(function() {
$("#SearchForm_SearchForm_Search").Watermark("Search the DEA");
})
})(jQuery);fixed it.
I have no idea why this worked, but it did.
If someone could shed some light on this for me, that would be appreciated.
-
Re: [Solved] JavaScript Watermark on Silver Stripe Search Textbox

17 July 2010 at 10:41am
Well thats called a javascript closure (http://doc.silverstripe.org/javascript#custom_jquery_code) and its used to encapsulate the $ to prevent it conflicting with prototype. You should write all your JS with that, though normally you would of had an error to your console if it was conflicting previously.
-
Re: [Solved] JavaScript Watermark on Silver Stripe Search Textbox

18 July 2010 at 5:06am
Cool. Thanks for your response. Now I know to always use the JavaScript enclosure and I should never have this problem again.
Cheers
-
Re: [Solved] JavaScript Watermark on Silver Stripe Search Textbox

25 September 2010 at 5:15am
I am having the same problem. I did manage to solve it thanks to your work on this, but had a few questions. I enclosed my custom .js files and that didn't fix the issue. I had jQuery 1.4.2 min included in Page.php. When I removed it from there and included it in Page.ss things started working.
Please forgive my ignorance, but why is this? Do I need to enclose the whole jQuery file or something?
| 1708 Views | ||
|
Page:
1
|
Go to Top |



