Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

jquery


Go to End


2 Posts   753 Views

Avatar
Adman

Community Member, 5 Posts

25 November 2012 at 7:51am

Hi guys,

I'd like to get custom jQuery working with my silverstripe install. I'm making a tenders class and want to do a bit of custom form field validation, but before I can do that I need to get the requires working and then my code inside the $(document).ready function working.

Here's what I have in my TenderHolder.php file

class TenderHolder_Controller extends Page_Controller {

public function init() {

parent::init();

Requirements::javascript ('mysite/javascript/jquery-latest.js');
Requirements::javascript ('mysite/javascript/test.js');

}

}

And here's what I have in my test.js file.

(function($) {
$(document).ready(function(){
// your code here.
alert('wassup');
})
})(jQuery);

I've also tried this for the test.js file

$(document).ready(function() {

alert('hello world');

});

I know the requires are working because if I don't surround the alert in jQuery document ready tags then it'll make an alert appear.

Any ideas would be great appreciated!

Thanks!

Avatar
Bigfork

Community Member, 23 Posts

4 December 2012 at 1:17am

I'm guessing that because it works without the jquery code, it's not loading jquery.

Requirements won't write the script tag in if the file doesn't exist so check the source, are there two <script> tags for your JS files near the bottom of the page?

If it is outputting them, check your JS console for errors, easily done with Web Developer in Firefox.

I always store JS files in themes/{name-of-theme} though I'm not sure if this was something I was told to do or just my preference. Can't see it would make a difference though.