0

I have a anchor tag and would like to add a class to it in my document ready function. How do I do that?

Thanks,

Sachin

1
  • 1
    if you want to have it from the start, why not add it on the html directly ? Commented May 15, 2012 at 10:40

3 Answers 3

3
$(function() {  
   /* domReady */
   $('a').addClass('yourclass');
});

of course if your link has an id use a more specific selector like

 $('#yourlink').addClass('yourclass');
Sign up to request clarification or add additional context in comments.

2 Comments

But the only thing is that I have 3 links and only want to add the class to one of them
@sachin, based on what rule ?
1
$(document).ready(function() {  
   $('#id1_of_a, #id2_of_a, #id3_of_a').addClass('myClass');
});

Comments

1
$(function() {  
   $('a:first').addClass('yourclass'); // suppose add class to first <a>
});

or

$('a:eq(1)').addClass('yourclass'); // suppose add class to second <a> and so on

or

$('a#some').addClass('yourclass'); // suppose add class to <a> have id 'some'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.