Insert HTML into Link within Div automatically with jQuery

1

I have an HTML code, where it has a link in an image. This link is created by a control panel and you can not put a ID or a Classe in it. But the image with the link is inside a div that has ID com name popup-imagem .

I would like to know if it is possible to add data-popup-open="popup-1" within the link through 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 would like you to stay:

<div id="popup-imagem">
    <a href="#" data-popup-open="popup-1"><img src="imagem.jpg">
    </a>
</div>
    
asked by anonymous 21.11.2016 / 18:51

2 answers

1

You can use jQuery's attr :

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

> a selects elements a where parent is popup-imagem .

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

    
21.11.2016 / 18:54
0
$( "#popup-imagem" ).find( "a" ).attr("data-popup-open", "popup-1")

See more at:

link

link

    
21.11.2016 / 18:54