Solved

Pass list box selected value and associated text to a js function

Posted on 2011-03-02
4
536 Views
Last Modified: 2012-08-13
Hello Experts,

I have a select:
<select id="myselect" onChange="parent.myfunction(selected value, selected option)">
<option value='abc'>123</option>
<option value='def'>456</option>
</select>

If the user selects 456. How do I pass 456 and the associated 'def' value.

Thanks
Dipen

0
Comment
Question by:jagku
  • 2
4 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35019918
Here's one way to do it:

<script>
function myfunction(thisSelect)
{
	alert('name: ' + thisSelect.options[thisSelect.selectedIndex].name)
	alert('value: ' + thisSelect.options[thisSelect.selectedIndex].value)
}
</script>

<select id="myselect" onChange="myfunction(this)">
<option name=123 value='abc'>123</option>
<option name=456 value='def'>456</option>
</select>

Open in new window

0
 
LVL 2

Assisted Solution

by:mhmr
mhmr earned 100 total points
ID: 35020049
Hi jaqku
here is a sample code
<!DOCTYPE html>
<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

  
 <script>
	$(document).ready(function() { 
		$('#myselect').change(function(){
			var txt = $(this).find('option:selected').text();
			var val = $(this).find('option:selected').val();
			alert(txt + '  -- >  ' + val);
		});
	});

 </script>
</head>
<body style="font-size:62.5%;">

<select id="myselect" onChange="parent.myfunction(selected value, selected option)">
	<option value='abc'>123</option>
	<option value='def'>456</option>
</select>

</body>
</html>

Open in new window

0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 400 total points
ID: 35020159
With an improvement thanks to mhmr:I forgot about the "text" attribute.

<script>
function myfunction(thisSelect)
{
	alert('text: ' + thisSelect.options[thisSelect.selectedIndex].text)
	alert('value: ' + thisSelect.options[thisSelect.selectedIndex].value)
}
</script>

<select id="myselect" onChange="myfunction(this)">
<option value='abc'>123</option>
<option value='def'>456</option>
</select>

Open in new window

0
 

Author Closing Comment

by:jagku
ID: 35020730
Perfect - many thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now