Drop down list value using javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpmagesh
    New Member
    • Nov 2008
    • 119

    Drop down list value using javascript

    Hi

    I am using php and smarty, what i have to do is i have a drop down box with dynamic name, id and values.

    like
    Code:
    <select name="id[{$options_data.ID}]" id="id[{$options_data.ID}]" onchange="sample()" >
      {foreach key=key_data item=item_data from=$options_data.DATA}
      <option value="{$item_data.ID}">{$item_data.TEXT} {if $item_data.PRICE !='' }{$item_data.PREFIX} {$item_data.PRICE}{/if} </option>
    {/foreach}
    </select>
    I want to take one value form that and i have to show in alert box, i tried some method but no use.

    Ex :
    Code:
    <select name ="id[{$options_data.ID}]" id="id[{$options_data.ID}]" onchange="sample()>
    <option value="45">value 1</option>
    <option value ="46">value 2</option>
    </select>

    Javascript code on top of the html page:
    Code:
    <script language="javascript">
    function sample()
    {
     var id = document.getElementById("id[3]");
    
     alert("The content of the first " + "\"" + id.innerHTML + "\".");
    }
    </script>
    But this showing all the values in the drop down box. i want to take only one value from the drop down..

    can anyone help me what will be the correction in this please.

    regards
    magesh
  • xNephilimx
    Recognized Expert New Member
    • Jun 2007
    • 213

    #2
    If "one value" means, "the selected option's inner html", you shoud do this:

    Code:
    id.options[id.selectedIndex].innerHTML
    if you need the value, just change the innerHTML part with value.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Note that IDs should not have [], so remove them and access as "id3".

      Comment

      • phpmagesh
        New Member
        • Nov 2008
        • 119

        #4
        Originally posted by xNephilimx
        If "one value" means, "the selected option's inner html", you shoud do this:

        Code:
        id.options[id.selectedIndex].innerHTML
        if you need the value, just change the innerHTML part with value.

        Hi

        Every time my id will be changed depends on the selection of category.

        Example

        Code:
        some time i will get that as <select name="id[3]" id="id[3]">
        some times i will get id[4], id[5], id[6] etc...

        so my select option name is not constant.

        now the problem is i want to take that name to that sample() function

        that name="id[{$options_data. ID}]". in this {$options_data. ID} is smarty value comes form php page i just want the values which is in select name


        Can anyone help me in this please.

        Regards
        magesh

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          For the ID use:
          Code:
          <select id="id{$options_data.ID}" onchange="sample()" >
          Then in the JavaScript code
          Code:
          function sample()
          {
           var id = document.getElementById("id{$options_data.ID}").value;
          if it's on the same page.

          Comment

          Working...