Megha Paul Megha Paul - 7 months ago 21
Javascript Question

How to find anchor tag in div and add class?

I have a particular div class in which there is anchor tag with no class and i have to add class to it using jquery.
This is sample html

<div id="mydiv"><a href="www.google.com">myclass</a></div>


This is what i am trying but not working

$('.myclass','a').attr('class','myclass');


Thanks in advance.

Answer

You need to use .addClass():

$('#mydiv a').addClass('myclass');

Working Fiddle