Learn how to a build a cloud-first strategyRegister Now

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

JavaScript/jQuery: find option by value

Using JavaScript/jQuery I want find which option has the value of 102.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>

<h1>Using JavaScript/jQuery I want find which option has the value of 102.

<div id="xyz">
 <seelect id="templateSelectOptions">
  <option value="100">Zooph</option>
  <option value="101">Foo</option>
  <option value="102">Quack</option>
  <option value="103">Oink</option>
 </select>
</div>

<script type="text/javascript">
/*<![CDATA[*/

alert('The option with a value of 102 is: ');

/*]]>*/
</script>

</body>
</html>

Open in new window

0
hankknight
Asked:
hankknight
  • 2
2 Solutions
 
leakim971PluritechnicianCommented:
For example : $("#templateSelectOptions>option[value='102']").text()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>

<h1>Using JavaScript/jQuery I want find which option has the value of 102.

<div id="xyz">
 <select id="templateSelectOptions">
  <option value="100">Zooph</option>
  <option value="101">Foo</option>
  <option value="102">Quack</option>
  <option value="103">Oink</option>
 </select>
</div>

<script type="text/javascript">
/*<![CDATA[*/

alert('The option with a value of 102 is: ' + $("#templateSelectOptions>option[value='102']").text() );

/*]]>*/
</script>

</body>
</html>

Open in new window

0
 
Greg AlexanderLead DeveloperCommented:
Here
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

<script>

</script>
</head>
<body>

<h1>Using JavaScript/jQuery I want find which option has the value of 102.

<div id="xyz">
 <select id="templateSelectOptions">
  <option value="100">Zooph</option>
  <option value="101">Foo</option>
  <option value="102">Quack</option>
  <option value="103">Oink</option>
 </select>
</div>

<script type="text/javascript">
/*<![CDATA[*/

$(document).ready(function(){
	
	alert($("#templateSelectOptions").find("option").val('103').val())
	
});
/*]]>*/
</script>

</body>
</html>


<script>

</script>

Open in new window

0
 
leakim971PluritechnicianCommented:
Thanks for the points!
0
 
David S.Commented:
@galexander07  Your code sets all of the options to the same value and then reads the new value from the first option.

Here's another way to do this:
$("#templateSelectOptions").children("[value='102']").text()

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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