473,668 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return a value of the selected item from a dropdown listbox

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 (objSelect.leng th);
alert (objSelect.valu e);
for (i=0; i<objSelect.len gth; i++) {
alert("recahed here.."+i)
alert (objSelect[i].value);
if (objSelect[i].selected) {
return objSelect[i].value;
}
}
return "";
}

here is corresponding html:

<form name="main_form ">
<table>
<tr><td>
<select name="assigned_ num">
<option value='null'>-- Select Number --</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>&nbsp;
<input type="button" class="button" value="Add"
onclick="javasc ript:getSelecte dItem('assigned _num:
'+document.main _form.assigned_ num.value);">
</td></tr>

</table>
</form>

What s wrong with this code and what s the correct implementation for
retrieving a selected value from the listbox( or dropdown listbox)?

Thanks
A
Mar 9 '06 #1
1 25718
acord wrote:
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 (objSelect.leng th);
alert (objSelect.valu e);
for (i=0; i<objSelect.len gth; i++) {
alert("recahed here.."+i)
alert (objSelect[i].value);
if (objSelect[i].selected) {
return objSelect[i].value;
}
}
return "";
}

here is corresponding html:

<form name="main_form ">
<table>
<tr><td>
<select name="assigned_ num">
<option value='null'>-- Select Number --</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>&nbsp;
<input type="button" class="button" value="Add"
onclick="javasc ript:getSelecte dItem('assigned _num:
'+document.main _form.assigned_ num.value);">
</td></tr>

</table>
</form>

What s wrong with this code and what s the correct implementation for
retrieving a selected value from the listbox( or dropdown listbox)?

Thanks
A


Hi,

To get the selected one use selectedIndex property of the select-object.
Thus:

<form name="fake">
<select name="theSelect or" onChange="alert It();">
<option value="apple">a pples
<option value="pear">pe ars
<option value="donut">d onuts
</select>
</form>

<script type="text/javascript">
function alertIt(){
var TheSelectedInde x = document.forms. fake.theSelecto r.selectedIndex ;
var TheSelectedValu e =
document.forms. fake.theSelecto r[TheSelectedInde x].value;
var TheSelectedText =
document.forms. fake.theSelecto r[TheSelectedInde x].text;
alert ("index = "+TheSelectedIn dex );
alert ("correspond ing option-value = TheSelectedValu e );
alert ("correspond ing option-text= "+TheSelectedTe xt );
}
</script>
Good luck.

Regards,
Erwin Moller
Mar 9 '06 #2

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

Similar topics

5
5246
by: Coz | last post by:
Hi all, I'm looking for help again!!! I have been writing a page to update a database but now have another 'silly' problem with listbox's...Grrr... I'm trying to populate the list box with static values pre-programmed into the page and then set the listbox value to that of the one contained in the database for that particular record the code I'm using is - <select name="SelectGarage">
5
5911
by: John O'Donnell | last post by:
Hello on my asp.net code I load up a listbox on the page_load method. The problem is that while the items I have added in the page_load do appear when i run the page, i am unable to get the value of the selected item using listbox.selectedvalue etc. somehow the server side is unaware that i have selected an item. In fact if i set the listbox.selecteditem=0; it always returns this item
6
5393
by: Jenna Alten | last post by:
I have a datagrid with a template column that contains a dropdown list. I currently fill and display the dropdown list on the page load. This is working correctly. I am NOT using an Edit Column. I am receiving errors when trying to set the selected value of the dropdown within the ItemDataBound() subroutine. Below is the code I am using. I hope someone can tell me if this is possible. '''HTML''' <asp:datagrid id="dgAssignments"...
2
4869
by: baret bonden | last post by:
Trying to return a selected listbox item to another form .tried lots of ways; defining public variables and passing those as well as textboxes ..I' m able to display the chosen item on it's form in a textbox and I'm able to pass other variables to the 2nd form, but not the data I want . I should add this is a smartdeviceapplication(if that matters; not sure) Here's most of the test code .
5
3250
by: Drew | last post by:
I am building an application for inserting and updating one field in a database. The database is in SQL Server and I am using Classic ASP. I have a dropdown listbox that is populated with names, when a name is selected it automatically submits the form and then replaces the dropdown listbox with the name of the resident selected. Now I need to take a field and split it into 2 textboxes. This is where my problems begin. The field is...
2
4546
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following:
2
3799
by: Greg | last post by:
I have a listbox inside of a DetailsView. There are three departments to choose from, Corporate, Group, Vacation. The listbox is set to SelectionMode="multiple". I think I have this loop setup correctly, the problem is as it is looping through it outputs the same value even though watching it validate each value - false, true, true - it repeats the value for the first "true" item it comes across. For instance if I choose Group and...
1
10387
by: Ben | last post by:
I have a formview with a few dropdownlists (software version, database version, etc). When a software version is selected, the database version dropdownlist updates itself accordingly. When in edit mode, everything works fine - the selected value in each list defaults to the value from the database record (selectedvalue=bind...). However, in insert mode (insertitem), it is not working. I pass in a value on the query string that I would...
1
2291
by: Ted | last post by:
Here is a stored procedure I created in MySQL: CREATE PROCEDURE `sp_find_food`( IN search_string varchar(255) ) BEGIN DECLARE ss VARCHAR(257); SET ss = CONCAT('%',search_string,'%'); SELECT NDB_No,Long_Desc FROM food_des WHERE Long_Desc LIKE ss; END
0
8462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8658
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7405
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.