Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts

    retrieve the element's order

    What propriety can return the index (the order) of an element?

    Ex

    <html>
    <head>
    <script>
    function bla(obj){
    alert(obj.what_property_to_show_the_index?)
    }
    </script>
    </head>

    <body>
    <form>
    <input name="q0" type="checkbox" onclick="bla(this)">
    <input name="q0" type="checkbox" onclick="bla(this)">
    <input name="q0" type="checkbox" onclick="bla(this)">
    </form>
    </body>
    </html>

    If click the first, I expect to see 0, the second 1 and so on....
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You have to loop thru formObj.elements collection to find the match and then return the index
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You mean something like?:

    function bla(a){
    c = new Array()
    for(i=0;i<3;i++){
    c[i]=document.forms[0].elements[a.name][i].checked;
    if(c[i] == true){alert(i)}
    }
    }

    I knew that, but I was looking for smth simplier....
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    function bla(obj){
      var f = obj.form;
      for (var j=0;j<f.elements.length;j++){
         if (f.elements[j].name==obj.name) return j;
      }
      return -1; //does not exist
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yeap, thanks. Your code is a general approach and I see I can use it no matter the type of elements or events.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my odd suggestion , note mine alerts what index the clicked check box has. in the check box group of its own name

    Code:
     
    function bla(obj){
    var group=document.getElementsByName(obj.name);
    for(I=0;I<group.length;I++)
    {
      if(group[I]==obj)
    	{
    	  alert(I);
    	}
    }
    }

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yes, thanks. You guess I need a group reference. But I needed to have a code even for NS4, so I had to search for a classical way, not a DOM method. So I did it this way

    Here's the final code for I needed that index:

    PHP Code:
    <html>
    <
    head>
    <
    script>
    var 
    nq 3;//input the number of questions
    var nc 3;//input the number of checkboxes on a question
    //no need to modify below
    var x;
    function 
    bla(a){
    = new Array()
    for(
    i=0;i<nc;i++){
    c[i]=document.forms[0].elements[a.name][i].checked;
    if(
    c[i] == true){x=i}
    }
    = new Array()
    for(
    j=0;j<nq*nc;j++){
    n[j]= document.forms[0].elements[j].name;
        for(
    i=0;i<nc;i++){
            if(
    i==x){
            
    document.forms[0].elements[n[j]][i].disabled true;    
            }
    }
    }    
    }
    </script>
    </head>

    <body>
    <form >
    Q1<br><br>
    <input name="q0" type="checkbox" onclick="bla(this)">
    <input name="q0" type="checkbox" onclick="bla(this)">
    <input name="q0" type="checkbox" onclick="bla(this)"><br>
    <br>
    Q2<br><br>
    <input name="q1" type="checkbox" onclick="bla(this)">
    <input name="q1" type="checkbox" onclick="bla(this)">
    <input name="q1" type="checkbox" onclick="bla(this)">
    <br>
    <br>
    Q3<br><br>
    <input name="q2" type="checkbox" onclick="bla(this)">
    <input name="q2" type="checkbox" onclick="bla(this)">
    <input name="q2" type="checkbox" onclick="bla(this)">
    </form>
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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