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

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

jQuery: Change background for all matching list items

I want all items in the first set with a match in the second set to have a red background.

These two lines should have a red background:

-  Foobar | 12 Live | 5 in process  | 2 inactive  
-  Quack | 11 Live | 72 in process  | 4 inactive  
<!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>I want all items in the first set with a match in the second set to have a red background.</h1>

<h2>Set 1</h2>

<ul id="dynoList1">
 <li>
  <a href="#" class="item">Foobar</a> 
  |
  <a href="#">12 Live</a> 
  |
  <a href="#">5 in process</a>
  |
  <a href="#">2 inactive</a>
 </li>
 <li>
  <a href="#" class="item">Moo</a> 
  |
  <a href="#">32 Live</a> 
  |
  <a href="#">12 in process</a>
  |
  <a href="#">0 inactive</a>
 </li>
 <li>
  <a href="#" class="item">Quack</a> 
  |
  <a href="#">11 Live</a> 
  |
  <a href="#">72 in process</a>
  |
  <a href="#">4 inactive</a>
 </li>
</ul>

<hr />

<h2>Set 2</h2>

<ul id="dynoList2">
 <li>
  <a href="#" class="item">Foobar</a> 
  |
  <a href="#">12 Live</a> 
  |
  <a href="#">3 in process</a>
  |
  <a href="#">2 inactive</a>
 </li>
 <li>
  <a href="#" class="item">Zooph</a> 
  |
  <a href="#">32 Live</a> 
  |
  <a href="#">12 in process</a>
  |
  <a href="#">0 inactive</a>
 </li>
 <li>
  <a href="#" class="item">Quack</a> 
  |
  <a href="#">11 Live</a> 
  |
  <a href="#">21 in process</a>
  |
  <a href="#">4 inactive</a>
 </li>
</ul>

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

$('#dynoList2 li').each(function(index) {
 var TitleLink = $(this).find('a').first().html();
 alert('Look for a match containing '+TitleLink + ' in the first set and make the background red if there is a match');
 $("#dynoList1 li a:contains(TitleLink):first").last().parent().css('background','red');
});

/*]]>*/
</script>

</body>
</html>

Open in new window

0
hankknight
Asked:
hankknight
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Just consider you need to pass the value of TitleLink, not "TitleLink" itslef : $("#dynoList1 li a:contains('" + TitleLink + "'):first").last().parent().css('background','red');

Not tested but should work :
<!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>I want all items in the first set with a match in the second set to have a red background.</h1>

<h2>Set 1</h2>

<ul id="dynoList1">
 <li>
  <a href="#" class="item">Foobar</a> 
  |
  <a href="#">12 Live</a> 
  |
  <a href="#">5 in process</a>
  |
  <a href="#">2 inactive</a>
 </li>
 <li>
  <a href="#" class="item">Moo</a> 
  |
  <a href="#">32 Live</a> 
  |
  <a href="#">12 in process</a>
  |
  <a href="#">0 inactive</a>
 </li>
 <li>
  <a href="#" class="item">Quack</a> 
  |
  <a href="#">11 Live</a> 
  |
  <a href="#">72 in process</a>
  |
  <a href="#">4 inactive</a>
 </li>
</ul>

<hr />

<h2>Set 2</h2>

<ul id="dynoList2">
 <li>
  <a href="#" class="item">Foobar</a> 
  |
  <a href="#">12 Live</a> 
  |
  <a href="#">3 in process</a>
  |
  <a href="#">2 inactive</a>
 </li>
 <li>
  <a href="#" class="item">Zooph</a> 
  |
  <a href="#">32 Live</a> 
  |
  <a href="#">12 in process</a>
  |
  <a href="#">0 inactive</a>
 </li>
 <li>
  <a href="#" class="item">Quack</a> 
  |
  <a href="#">11 Live</a> 
  |
  <a href="#">21 in process</a>
  |
  <a href="#">4 inactive</a>
 </li>
</ul>

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

$('#dynoList2 li').each(function(index) {
 var TitleLink = $(this).find('a').first().html();
 alert('Look for a match containing '+TitleLink + ' in the first set and make the background red if there is a match');
 $("#dynoList1 li a:contains('" + TitleLink + "'):first").last().parent().css('background','red');
});

/*]]>*/
</script>

</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
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.

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