views:

968

answers:

4

Hi. This seems to be a simple problem, but I dont use alot of javascript.

I have a div, with a checkbox in it, and would like the whole div to toggle the checkbox. This is what I have so far:

<div style="padding: 2em; border: 1px solid"
     onClick="if (document.getElementById('cb').checked) document.getElementById('cb').checked=false; else document.getElementById('cb').checked=true;">
  <input name="cb" id="cb" type="checkbox">
</div>

Only problem is when you click the actual checkbox, it get toggled twice, and therefore doesn't change.

Any ideas?

+1  A: 
onclick="if (event.target.tagName != 'INPUT') document.getElementById('cb').checked = !document.getElementById('cb').checked"
Alex Barrett
Thanks, I knew it would something simple like that!I did a similar thing, but stuffed it up :)
occhiso
+4  A: 

It's possible you could implement this is a more robust and accessible way by using the label element to wrap the area you want clickable. eg.

<label for="cb">
    <div style="padding: 2em; border: 1px solid">
        <input name="cb" id="cb" type="checkbox">
    </div>
</label>

I haven't tested the above code, but I believe all browsers support clicking of labels to check an input box.

Andy Hume
Im fairly new to web stuff, and I've actually never used <label for="">. If it is neater than having an onClick attribute on both the <div> and the <input> that could be good, but im not sure how to use this.
occhiso
I just read a related article which explains it here:http://stackoverflow.com/questions/2123/how-do-i-make-a-checkbox-toggle-from-clicking-on-the-text-label-as-wellThanks, I think I'll experiment with this.
occhiso
You can even put the style attributes into the label element itself and trash the div altogether.
Zack Mulgrew
I cant seem to get the label to span across the whole <div>, it only spans across the actual label of the checkbox.I actually intend to have an image inside of the div as well as the checkbox, but cant get the label to cover all of this.Thanks anyway, that would have been cleaner.
occhiso
AH, did it, will post a small example soon :) Thanks to all
occhiso
I needed display: block; and replaced the div with the label: <label for="test" style="padding: 15px; border: 1px solid; display: block; background-color: #e0e0ff;"> <img src="bla.gif" /><br /> <input type="checkbox" id="test" />A ticky box! </label>
occhiso
A: 

look into using jQuery rather than programming against the dom yourself. using a library like jQuery means your code is more likely to work on most browsers

http://docs.jquery.com/Effects/toggle

Damien McGivern
A: 

The root of the problem is that when you click on the checkbox, the click event is propagated up the DOM to parent elements.

So the checkbox handles the click event by toggling itself, and then the DIV receives the click event and toggles the checkbox again.

The simplest solution would be to stop propagation of the event, by adding this to the input element:

onClick="event.stopPropagation();"

While this is defined in the W3C DOM Level 2 Event Model, it may not be supported in all browsers so you may want to use a cross-browser library like Prototype or jQuery to ensure compatibility.