424,303 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

Drop down list value using javascript

100+
P: 119
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
Expand|Select|Wrap|Line Numbers
  1. <select name="id[{$options_data.ID}]" id="id[{$options_data.ID}]" onchange="sample()" >
  2.   {foreach key=key_data item=item_data from=$options_data.DATA}
  3.   <option value="{$item_data.ID}">{$item_data.TEXT} {if $item_data.PRICE !='' }{$item_data.PREFIX} {$item_data.PRICE}{/if} </option>
  4. {/foreach}
  5. </select>
  6.  
I want to take one value form that and i have to show in alert box, i tried some method but no use.

Ex :
Expand|Select|Wrap|Line Numbers
  1. <select name ="id[{$options_data.ID}]" id="id[{$options_data.ID}]" onchange="sample()>
  2. <option value="45">value 1</option>
  3. <option value ="46">value 2</option>
  4. </select>

Javascript code on top of the html page:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function sample()
  3. {
  4.  var id = document.getElementById("id[3]");
  5.  
  6.  alert("The content of the first " + "\"" + id.innerHTML + "\".");
  7. }
  8. </script>
  9.  
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
Jan 9 '09 #1
Share this Question
Share on Google+
4 Replies


xNephilimx
Expert 100+
P: 213
If "one value" means, "the selected option's inner html", you shoud do this:

Expand|Select|Wrap|Line Numbers
  1. id.options[id.selectedIndex].innerHTML
  2.  
if you need the value, just change the innerHTML part with value.
Jan 9 '09 #2

acoder
Expert Mod 15k+
P: 16,027
Note that IDs should not have [], so remove them and access as "id3".
Jan 9 '09 #3

100+
P: 119
@xNephilimx

Hi

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

Example

Expand|Select|Wrap|Line Numbers
  1. some time i will get that as <select name="id[3]" id="id[3]">
  2.  
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
Jan 10 '09 #4

acoder
Expert Mod 15k+
P: 16,027
For the ID use:
Expand|Select|Wrap|Line Numbers
  1. <select id="id{$options_data.ID}" onchange="sample()" >
Then in the JavaScript code
Expand|Select|Wrap|Line Numbers
  1. function sample()
  2. {
  3.  var id = document.getElementById("id{$options_data.ID}").value;
if it's on the same page.
Jan 10 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.