www.webdeveloper.com
Results 1 to 3 of 3

Thread: Checkboxes using onclick

  1. #1
    Join Date
    Oct 2005
    Posts
    59

    Checkboxes using onclick

    I have this script where, when you click the checkbox, the text next to it is highlighted.

    I need it to do that for a whole list of checkboxes. (When you click checkbox 1, text 1 highlights, when you click checkbox 2, text 2 highlights, etc.) But I can't get the syntax right. Here is the code:

    Code:
    <script type="text/javascript"> 
    
       function highlight3(myform, Class) {
            
          //Create Array of All HTML Tags
          var allHTMLTags = document.getElementsByTagName("*");
    
          //Loop through all tags using a for loop
          for (i=0; i < allHTMLTags.length; i++) {
    
             //Get all tags with the specified class name.
             if (allHTMLTags[i].className == Class) {
    
                if(myform.mycheckbox1.checked == true) {
                   allHTMLTags[i].style.backgroundColor = '#C39';
                }
                else {
                   allHTMLTags[i].style.backgroundColor = 'transparent';
                }
             }
          }
       }
    
    </script> 
    
    <form name="myform">
    
    <input type="checkbox" name="mycheckbox1" value="value" onclick="highlight3(this.form, 'highlight')">
    <span class="highlight"> Highlight this text by checking the box </span>
    Here it is in action: http://littleduck.com/test2.html

    So I want to have more checkboxes named mycheckbox2, mycheckbox3, etc. How can I add code in the script to cycle through all the checkboxes?

    Thank you.

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,683
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script  type="text/javascript">
    /*<![CDATA[*/
    
    function HighLight(obj,id){
     var h=document.getElementById(id);
     if (h){
      h.style.backgroundColor=obj.checked?'#FF0000':'transparent';
     }
    }
    /*]]>*/
    </script></head>
    
    <body>
    <input type="checkbox" name="mycheckbox1" value="value" onclick="HighLight(this, 'highlight1')">
    <span id="highlight1"> Highlight this text by checking the box </span>
    <input type="checkbox" name="mycheckbox1" value="value" onclick="HighLight(this, 'highlight2')">
    <span id="highlight2"> Highlight this text by checking the box </span>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Oct 2005
    Posts
    59
    It works! Thank you very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles