Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JS does not refresh 6 30
how can i make 2 columns? 4 36
Javascript 2 19
How to control cache of some js files ? 7 37
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

791 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