?
Solved

jQuery: Add class where link is...

Posted on 2011-10-29
2
Medium Priority
?
330 Views
Last Modified: 2012-05-12
Using jQuery, I want to add the class "selected" to all links that link to "#page3"
<!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>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<style type="text/css">
a.selected {
background: red;
}
</style>
</head> 
<body onload="initialize()"> 

 <p id="menu">
  <a href="#page1" class="link">1</a>
  <a href="#page2" class="link">2</a>
  <a href="#page3" class="link">3</a>
  <a href="#page4" class="link">4</a>
 </p>

<script type="text/javascript"> 

 // Add the class "selected" to the link that links to #page3

</script> 

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 11

Accepted Solution

by:
MacAnthony earned 1800 total points
ID: 37051590
Try this:

$(document).ready(function () {
    $('a[href=#page3]').addClass('selected');
});

Open in new window

0
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 200 total points
ID: 37052023
check this
$(document).ready(function(){
$("p a[href=#page3").addClass("selected");
$});
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 Month14 days, 7 hours left to enroll

807 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