breezy
breezy

Reputation: 1918

How to add and change a class on the body tag/div

I want to add a class on the body tag/div and on the anchor tag when I click an anchor tag.

Here is my jsFiddle File.

<div id="body">
 <!-- background image here -->

</div>

<ul>
    <li><a href="#">First Option</a></li>
    <li><a href="#">Second Option</a></li>
    <li><a href="#">Third Option</a></li>
</ul>​

Upvotes: 0

Views: 219

Answers (3)

vikas devde
vikas devde

Reputation: 11751

here it is, ad class to body and anchor itself http://jsfiddle.net/testtracker/4nFUd/2/

$('ul li a').click(function(){
$('#body').addClass('classname-tobody');
$(this).addClass('classname-toanchor');
});​

Upvotes: 0

bluetoft
bluetoft

Reputation: 5443

http://jsfiddle.net/4nFUd/5/

Add an event listener to the click and modify as needed...

<script>
$('ul').on('click','a',function(){
    $('#body').css('background',$(this).attr('rel'));
    $(this).addClass($(this).attr('rel'));
});
</script>
<div id="body">
 <!-- background image here -->

</div>

<ul>
    <li><a href="#" rel="blue">First Option</a></li>
    <li><a href="#" rel="green">Second Option</a></li>
    <li><a href="#" rel="yellow">Third Option</a></li>
</ul>

Upvotes: 2

Sven Bieder
Sven Bieder

Reputation: 5681

That should do the trick.

$("li a").click(function(){
  $(this).addClass("class1");
  $("#body").addClass("class2");
});

Upvotes: 0

Related Questions