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

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

jQuery: Make third link green

This code makes all my links green.  I only want the third link to be green.
<p id="myp"> 
   <a href="#i1">Link 1</a> 
   <a href="#i2">Link 2</a> 
   <a href="#i3">Link 3</a> 
   <a href="#i4">Link 4</a> 
  </p>

<script type="text/javascript">
  $("#myp a").css("color", "#11ff11");
</script>

Open in new window

0
hankknight
Asked:
hankknight
  • 2
1 Solution
 
rstjeanCommented:
<p id="myp">
   <a href="#i1" class=l1>Link 1</a>
   <a href="#i2" class=l1>Link 2</a>
   <a href="#i3" class=l2>Link 3</a>
   <a href="#i4" class=l1>Link 4</a>
  </p>

<script type="text/javascript">
  $("#myp l1").css("color", "#11ff11");
</script>

I don't have my script up and running but form off the top of my head, this should work.  Make sure to declare the classes in the .css file
0
 
0rikCommented:
This will make only your third link green.
<p id="myp"> 
   <a href="#i1">Link 1</a> 
   <a href="#i2">Link 2</a> 
   <a href="#i3">Link 3</a> 
   <a href="#i4">Link 4</a> 
  </p>

<script type="text/javascript">
  $("#i3").css("color", "#11ff11");
</script>

Open in new window

0
 
leakim971PluritechnicianCommented:
Use : http://api.jquery.com/eq-selector/

So : $("#myp a:eq(2)").css("color", "#11ff11");

test page :


<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
</head>
<body>
<p id="myp"> 
   <a href="#i1">Link 1</a> 
   <a href="#i2">Link 2</a> 
   <a href="#i3">Link 3</a> 
   <a href="#i4">Link 4</a> 
  </p>
<script type="text/javascript">
  $("#myp a:eq(2)").css("color", "#11ff11");
</script>
</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
Thanks for the points!
0

Featured Post

Technology Partners: 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