-2

Possible Duplicate:
Select All Checkbox

I have the follow code for a Check All button that will check all my checkboxes on my webpage, but it's not working. Where is the function supposed to go? Because I've tried putting the code on my page but it doesn't work.

http://jsfiddle.net/KbkZk/1/

Sorry lol, been a long day!!

Community
  • 1
  • 1
James
  • 1
  • 5
    You apparently forgot to add the code... – fvu Aug 31 '11 at 20:17
  • You forgot the code James. Or you forgot to write it and that's why it does nothing :p – Kheldar Aug 31 '11 at 20:18
  • maybe you forgot to add code to your page also ? – Igoris Azanovas Aug 31 '11 at 20:18
  • What is the difference between this and the duplicate question? – Pekka Aug 31 '11 at 20:20
  • JS Fiddle's options prevent the code from loading unless dom is ready. this could be why the code works in the JS Fiddle. As such Marc's answer below is correct, you just need to put your code in the `ready` function to ensure the dom is ready to be manipulated. – Ali Aug 31 '11 at 20:30

1 Answers1

1

What code? Generally you put your JS in the <head> block of your page. If the JS will be manipulating the page's contents, you'll need to delay its execution until the rest of the page is loaded. In jquery, you do that with:

<script>
$(document).ready(function() {
    ... your code here ...
}):
</script>

If you're not using a JS library, you can do mostly the same thing with:

<body onload="myFunction();">

and have that myFunction do your initialization/setup.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • the current wisedom is to put all your javascript before the close of the `body` tag and all style sheets in the `head`. The reason for this is that more often than not, you want the content of your page to render and you don't want this slowed down because large JS libraries are loading. Style sheets go in the head because you do want your content to have it's styles when it's rendered. Putting JS before the close of the `body` tag also helps with SEO. – Ali Aug 31 '11 at 20:28