Array of listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajiv07
    New Member
    • Jun 2007
    • 141

    Array of listbox

    Hi I am using an array of listboxes .here i want to get the index of the listbox when it get changed.I have spent 1 Hour for this problem but i could not sort this problem.

    Please Help me on this.

    Thanks
    Rajiv
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    please post the code you have so far ...

    kind regards

    Comment

    • rajiv07
      New Member
      • Jun 2007
      • 141

      #3
      Originally posted by gits
      hi ...

      please post the code you have so far ...

      kind regards
      Thanks gits

      [CODE=javascript]
      var position;
      for(i=0;i<=docu ment.form1.ref_ type.length;i++ ){
      if(document.for m1.ref_type[i].change==true){
      position=i; }
      }
      [/CODE]

      But this is not working.

      Thanks

      Rajiv

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        that will not work that way. how do you construct the array of your listboxes and whatfor do you need the index to be retrieved? lets sort out this together ... the idea would be:

        1. don't use an array but an object
        2. onchange of the select-node we retrieve its position in the list

        kind regards

        Comment

        • rajiv07
          New Member
          • Jun 2007
          • 141

          #5
          Originally posted by gits
          that will not work that way. how do you construct the array of your listboxes and whatfor do you need the index to be retrieved? lets sort out this together ... the idea would be:

          1. don't use an array but an object
          2. onchange of the select-node we retrieve its position in the list

          kind regards
          Please could you post any sample code or sites to refer form this.

          Thanks

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            ok assuming we use the following code:

            [CODE=javascript]var list = document.getEle mentsByTagName( 'select');
            var lookup = {};

            for (var i = 0, n; n = list[i]; i++) {
            lookup[n.name] = i;
            }
            [/CODE]
            lookup now looks like this pseudocode:

            Code:
            { select_name: select_index_in_list }
            now you could use a function like this in the onchange of the select-node:

            [CODE=javascript]function get_idx(node) {
            return lookup[node.name];
            }[/CODE]
            [HTML]<select name="my_name" onchange="get_i dx(this);">[/HTML]
            where the function returns you the index in list

            kind regards

            Comment

            Working...