Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find anchor tag in div and add class?

Tags:

jquery

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.

like image 355
Megha Paul Avatar asked Sep 15 '25 01:09

Megha Paul


1 Answers

You need to use .addClass():

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

Working Fiddle

like image 101
Milind Anantwar Avatar answered Sep 17 '25 20:09

Milind Anantwar