RLJ Posted December 30, 2010 Share Posted December 30, 2010 Hi, probably a very straightforward problem, but here it is: <html> <head> <script language='JavaScript'> function enable3(moreoptions) { var elements = document.getElementsByName("txt"); var length = elements.length; for (var i = 0; i < length; i++) { //if(document.getElementById(moreoptions).checked == true) {elements[i].style.color='red';} //else //{elements[i].style.color='blue';} } } </script> </head> <body> <label for="moreoptions"><input type="checkbox" name="moreoptions" id="moreoptions" onchange="enable3(moreoptions)"> Show additional options</label><p></p> <span id ="span1" name="txt">SPAN1</span> <span id ="span2" name="txt">SPAN2</span> <span id ="span3" name="txt">SPAN3</span> </body> </html> I want to use the above code - when checkbox 'moreoptions' is clicked - to check whether the checkbox is checked or not and, depending on the result, make elements with name 'txt' either blue or red. With parts of the code commented out, as above, it works fine and turns the elements red when the checkbox is clicked. However, when those parts are not commented out (i.e. the checking whether the checkbox is checked or not part is enabled), the elements with name 'txt' don't change color. Can someone see where I've gone wrong?? Much appreciated! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/223036-simple-check-whether-checkbox-is-checked-and-do-something-not-working/ Share on other sites More sharing options...
brianlange Posted December 31, 2010 Share Posted December 31, 2010 Here's a working example http://gonavygolf.com/test8.html. View the source to see the code. Use onclick instead of onchange. Pass 'this' to the onclick function. There's no need for the {} when referencing the elements array. Quote Link to comment https://forums.phpfreaks.com/topic/223036-simple-check-whether-checkbox-is-checked-and-do-something-not-working/#findComment-1153252 Share on other sites More sharing options...
RLJ Posted December 31, 2010 Author Share Posted December 31, 2010 Cheers for the reply. However, your link's not working, 404 page not found. Also, since I have a label for the checkbox, isn't onchange better than onclick? Because the checkbox can change even if it is not directly clicked on. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/223036-simple-check-whether-checkbox-is-checked-and-do-something-not-working/#findComment-1153374 Share on other sites More sharing options...
Pikachu2000 Posted December 31, 2010 Share Posted December 31, 2010 Remove the trailing period from the URL. Quote Link to comment https://forums.phpfreaks.com/topic/223036-simple-check-whether-checkbox-is-checked-and-do-something-not-working/#findComment-1153376 Share on other sites More sharing options...
RLJ Posted December 31, 2010 Author Share Posted December 31, 2010 Thanks! That's great. Quote Link to comment https://forums.phpfreaks.com/topic/223036-simple-check-whether-checkbox-is-checked-and-do-something-not-working/#findComment-1153392 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.