Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

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

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
jagku
Asked:
jagku
  • 2
2 Solutions
 
sjklein42Commented:
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
 
mhmrCommented:
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
 
sjklein42Commented:
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
 
jagkuAuthor Commented:
Perfect - many thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now