how to get the length of the choice box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manishamca
    New Member
    • Mar 2008
    • 17

    how to get the length of the choice box

    i want to find the length of the choice box.....i,e to find how many options are present in the choice box...
    for example if the number of options in the choice box is 4 then we have to find that the length of the choice box as 4 by using some javascript code....

    Thanx in advance
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    give your select node an id and use childNodes.leng th like this:

    [CODE=javascript]function get_child_nodes _length(id) {
    var sel_node = document.getEle mentById(id);
    return sel_node.childN odes.length;
    }
    [/CODE]
    kind regards

    Comment

    • manishamca
      New Member
      • Mar 2008
      • 17

      #3
      thanx for ur reply.....i will give u my javascript code plz help me.

      [CODE=javascript]function refresh()
      {

      var indexOfcombobox = document.form1. drughid.value;
      sel = document.form1. drug;
      for (i=0; i<sel.options.l ength; i++) {
      if (sel.options[i].text ==indexOfcombob ox ) {
      sel.selectedInd ex = i;
      }
      }
      }[/CODE]
      Here drug is the name of the select box.
      and Drughid is the name of the textbox.
      and now i have to compare the value of the textbox with the option of the choice box.
      And if the choice box option value becomes equal to the textbox value i have to set that index.
      I call the refresh function on "<BODY onload="refresh ()">"

      can u suggest me any solution.
      Actualy am geting error that
      "sel.option s .length" is null or not a object...

      plz give me some solution...
      Thanx in advance...
      Originally posted by gits
      give your select node an id and use childNodes.leng th like this:

      [CODE=javascript]function get_child_nodes _length(id) {
      var sel_node = document.getEle mentById(id);
      return sel_node.childN odes.length;
      }
      [/CODE]
      kind regards
      Last edited by gits; Mar 18 '08, 04:47 PM. Reason: added code tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Just set the value of the select box instead:
        [CODE=javascript]function refresh()
        {
        var indexOfcombobox = document.form1. drughid.value;
        document.form1. drug.value = indexOfcombobox ;
        }[/CODE]

        Comment

        Working...