Results 1 to 3 of 3

Thread: Checkboxes using onclick

  1. #1
    Join Date
    Oct 2005

    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:

    <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';
    <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
    Portsmouth UK
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <script  type="text/javascript">
    function HighLight(obj,id){
     var h=document.getElementById(id);
     if (h){
    <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>

    God loves you and will never love you less.

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

  3. #3
    Join Date
    Oct 2005
    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