473,406 Members | 2,220 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,406 software developers and data experts.

javascript function to return selected item value from drop-down

Friends,

I need to write a javascript function (which will be called on
clicking a button) to return the currently selected item from a drop-
down list whose rendered html is below.

<select name="ddlQuery"
onchange="javascript:setTimeout('__doPostBack(\'dd lQuery\',\'\')', 0)"
id="ddlQuery" style="width:273px;">
<option value="Munich">Munich</option>
<option selected="selected" value="London">London</option>
<option value="Paris">Paris</option>
<option value="Tokyo">Tokyo</option>
</select>

So in the above case, the javascript should return the string 'London'
which is the selected item.

Can you please advice?

Thanks,
PD

May 21 '07 #1
7 88001
VK
On May 21, 2:19 pm, pardesiya <zenst...@gmail.comwrote:
Friends,

I need to write a javascript function (which will be called on
clicking a button) to return the currently selected item from a drop-
down list whose rendered html is below.

<select name="ddlQuery"
onchange="javascript:setTimeout('__doPostBack(\'dd lQuery\',\'\')', 0)"
id="ddlQuery" style="width:273px;">
<option value="Munich">Munich</option>
<option selected="selected" value="London">London</option>
<option value="Paris">Paris</option>
<option value="Tokyo">Tokyo</option>
</select>

So in the above case, the javascript should return the string 'London'
which is the selected item.
<script type="text/javascript">
function __doPostBack(elm) {
var val = elm.options[elm.selectedIndex].value;
window.alert(val);
}
</script>
<select name="ddlQuery" id="ddlQuery" onchange="__doPostBack(this)">
<option value="Munich">Munich</option>
<option selected="selected" value="London">London</option>
<option value="Paris">Paris</option>
<option value="Tokyo">Tokyo</option>
</select>

P.S. The intrinsic onchange handler gets a reference to Javascript
function, it has nothing to do with a link to javascript: pseudo-
protocol, thus onchange="javascript:something()" is a non-sense.

May 21 '07 #2
Thanks a lot VK. Sorry I wasnt very clear. We can ignore
onchange="javascript:....". If it's of any interest, that's just the
generated html from my asp.net aspx page. What I needed was the
required function to be called on click of a button.

Your solution has pointed me to the right direction and I am able to
achieve this as below:

<script type="text/javascript">
function fShowCity(elm)
{
var val = elm.options[elm.selectedIndex].value;
window.alert(val);
}
</script>

<select name="ddlQuery" id="ddlQuery" style="width:273px;">
<option value="Munich">Munich</option>
<option selected="selected" value="London">London</option>
<option value="Paris">Paris</option>
<option value="Tokyo">Tokyo</option>
</select>

<input type="submit" name="cmdShow" value="Customize Fields"
onclick="fShowCity(ddlQuery);" id="cmdShow" />


May 21 '07 #3
pardesiya wrote on 21 mei 2007 in comp.lang.javascript:
Thanks a lot VK. Sorry I wasnt very clear.
You are not very clear now, as you do not quote!!!!!!!!!!
This is usenet, not email.
We can ignore
onchange="javascript:....". If it's of any interest, that's just the
generated html from my asp.net aspx page. What I needed was the
required function to be called on click of a button.

Your solution has pointed me to the right direction and I am able to
achieve this as below:

<script type="text/javascript">
function fShowCity(elm)
elm is a local variable containing the litteral string 'ddlQuery',
and only under IE this is convered to an object pointer to your select.

to make it cross browser compatible write:

function fShowCity(elm2) {
var elm = document.getElementById('elm2');
alert(elm.options[elm.selectedIndex].value);
};
{
var val = elm.options[elm.selectedIndex].value;
window.alert(val);
}
</script>

<select name="ddlQuery" id="ddlQuery" style="width:273px;">
<option value="Munich">Munich</option>
<option selected="selected" value="London">London</option>
<option value="Paris">Paris</option>
<option value="Tokyo">Tokyo</option>
</select>

<input type="submit" name="cmdShow" value="Customize Fields"
onclick="fShowCity(ddlQuery);" id="cmdShow" />
ddlQuery is not a variable name but a string so it should be quoted.

onclick="fShowCity('ddlQuery');"
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 21 '07 #4
VK
On May 21, 2:58 pm, pardesiya <zenst...@gmail.comwrote:
<input type="submit" name="cmdShow" value="Customize Fields"
onclick="fShowCity(ddlQuery);" id="cmdShow" />
This way of calling function relies on IE-proprietary extension with
ID'ed element automatically reflected as global JScript variables:
http://www.jibbering.com/faq/index.html#FAQ4_41

While Gecko browsers in quirk mode do support this feature now as
well, you should not rely on it on a wide run. Use the fully universal
DOM 0 methods - the best - instead or the conventional DOM 1 methods:

<input type="button" name="cmdShow" value="Customize Fields"
onclick="fShowCity(this.form.elements['ddlQuery']);" id="cmdShow" />

or

<input type="button" name="cmdShow" value="Customize Fields"
onclick="fShowCity(document.getElementById('ddlQue ry'));"
id="cmdShow" />
May 21 '07 #5
On May 21, 5:26 am, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
to make it cross browser compatible write:

function fShowCity(elm2) {
var elm = document.getElementById('elm2');
alert(elm.options[elm.selectedIndex].value);

};
function fShowCity(elm2) {
var elm = document.getElementById(elm2);
alert(elm.options[elm.selectedIndex].value);

};

May 21 '07 #6
scripts.contact wrote on 21 mei 2007 in comp.lang.javascript:
On May 21, 5:26 am, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
>to make it cross browser compatible write:

function fShowCity(elm2) {
var elm = document.getElementById('elm2');
alert(elm.options[elm.selectedIndex].value);

};

function fShowCity(elm2) {
var elm = document.getElementById(elm2);
alert(elm.options[elm.selectedIndex].value);

};
Yes!
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 21 '07 #7
On May 21, 5:26 am, "Evertjan." <exjxw.hannivo...@interxnl.net>
wrote:
You are not very clear now, as you do not quote!!!!!!!!!!
This is usenet, not email.
Apologies again. I am a newbie in usenet. Will henceforth quote.
to make it cross browser compatible write:

function fShowCity(elm2) {
var elm = document.getElementById(elm2);
alert(elm.options[elm.selectedIndex].value);
};

Thanks for the cross browser compatible tip.
On May 21, 8:27 am, VK <schools_r...@yahoo.comwrote:
This way of calling function relies on IE-proprietary extension with
ID'ed element automatically reflected as global JScript variables:http://www.jibbering.com/faq/index.html#FAQ4_41

While Gecko browsers in quirk mode do support this feature now as
well, you should not rely on it on a wide run. Use the fully universal
DOM 0 methods - the best - instead or the conventional DOM 1 methods:

<input type="button" name="cmdShow" value="Customize Fields"
onclick="fShowCity(this.form.elements['ddlQuery']);" id="cmdShow" />

or

<input type="button" name="cmdShow" value="Customize Fields"
onclick="fShowCity(document.getElementById('ddlQue ry'));"
Thanks VK.

May 22 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan | last post by:
Is it possible to have a javascript function return a value to set a webpage elements property? Ie. <td bgcolor=get_background_colour()> Thanks for any help, Dan.
2
by: Stephen Miller | last post by:
When I dynamically populate a HtmlSelect combo box, the Value property consistently fails to return the item selected, defaulting instead to the first item in the list. For example: Protected...
5
by: David | last post by:
I want to get the Sel_142's multiple item's value, and put them in an array. if ( !Sel_142.Multiple ) vo.CA142 = Sel_142.Items.Value; else { Sel_142.Items..... }
1
by: acord | last post by:
Hi, I am having problem to get a value of the selected item from a dropdown listbox. Here is the JS function; function getSelectedItem(objSelect) { alert("in getSelectedItem"); alert...
3
by: imrantbd | last post by:
I need array type name like "destList" must use for my destlist select box,not a single name.Or need a solution to capture multiple value of "destList" select box and send all selected value in php...
3
by: Iain | last post by:
Hi All I have 2 DropDownList boxes on a page. The first (id= "Operation") is populated on PageLoad with the contents of a database table. The second id="WorkStations" will not be populated...
9
by: mtczx232 | last post by:
it's posible that Function return byref? what the syntax?
2
by: ameetijantakar | last post by:
Hi, I want to call a javascript function in attributes property of a RadioButtonList control when user changes his selection
0
by: bndprasad | last post by:
how to retrive javascript function return values into code behind
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.