Web Analytics Made Easy -
StatCounter How do I scan for a checkbox's name... - CodingForum

Announcement

Collapse
No announcement yet.

How do I scan for a checkbox's name...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I scan for a checkbox's name...

    I have a dynamically created HTML page which will have
    any number of checkboxes (all will start with "VJCDE-")

    How do I scan all the elements on the form to see if
    the name starts with VJCDE-

    I realize I'm using elements[]
    but how do I get the name of the element?

    George

  • #2
    Here are two methods. One using name and the other indexOf:
    Code:
    <form>
    <input type="checkbox" name="VJCDE-">
    </form>
    
    
    <script>
    var inputs = document.forms[0].getElementsByTagName('INPUT');			
    for(i=0; i<inputs.length;i++){
    if(inputs[i].getAttribute('TYPE')=='checkbox' && inputs[i].name == 'VJCDE-' ){
    alert('Ha-Ha You have been found! (1)')
    }
    
    if(inputs[i].getAttribute('TYPE')=='checkbox' && inputs[i].name.indexOf('VJCDE-') > -1 ){
    alert('Ha-Ha You have been found! (2)')
    }
    }
    
    </script>
    .....Willy

    Comment


    • #3
      My method was:

      'function checkall() {'
      ' var elen = document.FORM1.elements.length;'
      ' for (var i=0 ; i<elen ; i++) {'
      ' var ename = document.FORM1.elements[i].name;'
      ' if (ename.substring(0,6) == "VJCDE-") {'
      ..............
      ' }'
      ' }'
      ' return;'
      '}'

      ignore the surrounding 's

      Any reasons why not to do it this way? aside from personal choice.

      Thanks
      George

      Comment


      • #4
        aside from changing:

        ' var ename = document.FORM1.elements[i].name;'
        ' if (ename.substring(0,6) == "VJCDE-") {'

        to

        if (document.FORM1.elements[i].name.substring(0,6) == "VJCDE-")


        Since I was testing. it was easier to break things up into
        pieces, to see what worked and what didn't.

        George

        Comment


        • #5
          Whichever way you prefer and works for you sounds like a winner to me.

          .....Willy

          Comment

          Working...
          X