473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Drop down list value using javascript

119 100+
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
4 6999
xNephilimx
213 Expert 100+
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
16,027 Expert Mod 8TB
Note that IDs should not have [], so remove them and access as "id3".
Jan 9 '09 #3
phpmagesh
119 100+
@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
16,027 Expert Mod 8TB
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

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

Similar topics

4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
1
by: Dan | last post by:
This is one that has me stumped and I need an expert's input. Any ideas why the values from the second script-generated drop down list isn't recognized by the script to add time values to the...
3
by: Stephen Adam | last post by:
Hi there, I'm sure i'm missing something really simple here, all i want to do is get the value of the selected item in a list box. Even after much fiddling about last night I still could not get...
2
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will...
3
by: John Walker | last post by:
Hi, On an ASP.NET page I have a drop down list control. When the user pulls down the list and makes a selection, I perform validation, and if the validation fails I want the selected item in...
1
by: Jeff Gardner | last post by:
Greetings: I have a table with 3 pieces of data that I would like to use to dynamically populate 3 drop downs using javascript. The fields are state, orgname, office. If it's not already...
0
by: weiwei | last post by:
Hi here is my scenario, I create a drop down list in itemtemplate.(that drop down is created from db), after user click edit command, my ideal plan is have another drop down list in...
3
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the...
1
by: deerick | last post by:
Hi there I want to know how to add a drop down list based on value from a table in an asp form. Then I want the user to be able to submit the form, and the drop down list value that they have...
15
by: ajos | last post by:
Hello all, Im making a dependent drop down for my application for filtering purpose. I have populated the drop down from the database and retrieved in the html. <tr> <td valign="top"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.