Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JavaScript/jQuery: find option value by text

Posted on 2011-04-25
2
Medium Priority
?
853 Views
Last Modified: 2012-05-11
Using JavaScript/jQuery I want find the value of the first option with the text of "Zooph".
<!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 the value of the first option with the text of "Zooph".

<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 value of the first option with the text "Zooph" is: 100');

/*]]>*/
</script>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
2 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 35461206
using :  $("#templateSelectOptions>option:contains('Zooph'):first").val()
<!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>
  <option value="103">Zooph</option>
 </select>
</div>

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

alert('The value of the first option with the text "Zooph" is: ' + $("#templateSelectOptions>option:contains('Zooph'):first").val() );

/*]]>*/
</script>

</body>
</html>

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 35461259
Thanks for the points!
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.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

564 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