How to add target = _blank to all PDF links inside a div using jquery?

For instance:

I want to add target=_blankto any pdf link included in this css class"class="newWindow"

Before adding a script

<div class="newWindow" >

<a href="pdf1.pdf">link text</a>
<a href="pdf2.pdf">link text</a>

</div>

After adding the script

<div class="newWindow" >

<a href="Pdf1.pdf" target="_blank">link text</a>
<a href="Pdf2.pdf" target="_blank">link text</a>

</div>

Provide jquery code without conflict.

+3
source share
2 answers

The code

$(".newWindow a[href$='pdf']").attr('target','_blank');

:]

Preview:

http://jsbin.com/ojapo

View code

http://jsbin.com/ojapo/edit

Note:

in jsbin example, I also add a class .bl, so you can easily see the result:]

No conflicts:

jQuery.noConflict();
jQuery(".newWindow a[href$='pdf']").attr('target','_blank');

or

 jQuery.noConflict();

 jQuery(document).ready(function($){
   $(".newWindow a[href$='pdf']").attr('target','_blank');
 });

or in another way, look here: http://docs.jquery.com/Using_jQuery_with_Other_Libraries

+3
source

$(".newWindow a[href$='.pdf']").attr("target", "_blank");
0

Source: https://habr.com/ru/post/1734862/


All Articles