Solved

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

Posted on 2011-03-02
4
537 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

813 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

15 Experts available now in Live!

Get 1:1 Help Now