Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply anchor tag to all the h3 tag under a class

Tags:

html

jquery

css

Im trying to find a way to add a anchor tag to all h3 tag.The content is dynamic in nature .

I want to add dynamic anchor tag for all the h3 tags only

<div class="class1">
    <h2>Main heading</h2>
    <h3>Heading1</h3>
    <h1>some content1</h1>
    <h3>Heading2</h3>
    <h1>some content2</h1>
    <h3>Heading3</h3>
    <h1>some content3</h1>
</div>

I tried below Jquery code but its creating nothing

$("h3.class1").append('<h3>');
var al = $("h3.class1 >");

al.append('<a href="#"></a>');
like image 305
mark rammmy Avatar asked Dec 05 '25 05:12

mark rammmy


2 Answers

$('div.class1 h3').each(function (){
            $(this).wrap('<a />');
        });
like image 121
Nithin Krishnan P Avatar answered Dec 07 '25 18:12

Nithin Krishnan P


Use wrap:

Wrap an HTML structure around each element in the set of matched elements.

$('.class1 h3').wrap('<a />');

This will wrap all the h3 inside .class1 by anchor tag.

DEMO

like image 42
Tushar Avatar answered Dec 07 '25 19:12

Tushar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!