Solved

jQuery: Make third link green

Posted on 2010-09-20
4
207 Views
Last Modified: 2012-06-27
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
Comment
Question by:hankknight
  • 2
4 Comments
 
LVL 4

Expert Comment

by:rstjean
ID: 33721116
<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
 
LVL 1

Expert Comment

by:0rik
ID: 33721193
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
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33721271
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
 
LVL 82

Expert Comment

by:leakim971
ID: 33721308
Thanks for the points!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

809 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