Insert HTML into Link inside Div automatically with jQuery

Asked

Viewed 531 times

1

I have an HTML code, where it has a link in an image. This link is created by a control panel and it is not possible to place a ID or a Classe in it. But the image with the link is inside a div who has the ID by name popup-imagem.

I wonder if it is possible to add data-popup-open="popup-1" within the link via jQuery, and if possible, how do I do this?

Current Code:

<div id="popup-imagem">
    <a href="#"><img src="imagem.jpg">
    </a>
</div>

How I’d like you to stay:

<div id="popup-imagem">
    <a href="#" data-popup-open="popup-1"><img src="imagem.jpg">
    </a>
</div>

2 answers

1


You can use the attr jQuery:

$("#popup-imagem > a").attr("data-popup-open","popup-1");

> a selects the elements a where the father is popup-imagem.

His syntax is: .attr( attributeName, value ), where the first argument is the name of the attribute and the second the value. The first is always a string, while the second can be a string, a number or null.

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.