We help IT Professionals succeed at work.

jQuery: element equals

hankknight
hankknight asked
on
Medium Priority
965 Views
Last Modified: 2012-08-14
What should I use instead of :contains for "equals"?
<!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>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>

<h1>The item below should be red because EQUALS "Hello".</h1>

<ul id="zooph1">
 <li><a>Hello</a></li>
</ul>

<h1>I do NOT want the item below to be red because it does not EQUAL Hello.  It contains Hello but it does not equal "Hello" </h1>

<ul id="zooph2">
 <li><a>Hello World</a></li>
</ul>

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

$("#zooph1 li a:contains('Hello')").first().parent().css('background','red');

$("#zooph2 li a:contains('Hello')").first().parent().css('background','red');

/*]]>*/
</script>

</body>
</html>

Open in new window

Comment
Watch Question

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You may use html instead contains
<!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>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>

<h1>The item below should be red because EQUALS "Hello".</h1>

<ul id="zooph1">
 <li><a>Hello</a></li>
</ul>

<h1>I do NOT want the item below to be red because it does not EQUAL Hello.  It contains Hello but it does not equal "Hello" </h1>

<ul id="zooph2">
 <li><a>Hello World</a></li>
</ul>

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

$("#zooph1 li a:contains('Hello')").first().parent().css('background','red');

$("#zooph2 li a:html('Hello')").first().parent().css('background','red');

/*]]>*/
</script>

</body>
</html>

Open in new window

Author

Commented:
leakim971, your idea does not work.

I get this error in Firefox:
Error: uncaught exception: Syntax error, unrecognized expression: Syntax error, unrecognized expression: html
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Thanks for the points!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.