JQuery Selection Question - How to find all HREF with target = _blank?

My "jQuery Selector Foo" stinks. I need to find all the HREFs with the target attr _blank and replace them with a common window / target. Help is much appreciated!

+6
source share
3 answers
$("a[target='_blank']").attr('target', ' sometarget '); 

Do you mean something like that?

+14
source

to try

  $("a[target=_blank]").each(function () { var href = $(this).attr("href"); // retrive href foreach a $(this).attr("href", "something_you_want"); // replace href attribute with wich u want // etc }); 

let me know what you want for more help

0
source

If you are specifically looking for href values ​​that have empty values, follow these steps

 $('a[href=""]').each(function() { $(a).attr('href', 'theNewUrl'); }); 

This will only capture anchor tags that have an href attribute that is empty. This will not work though for anchors that lack the href tag

 <a href="">Link 1</a> <!-- Works --> <a>Link 2</a> <!-- Won't work --> 

If you need to combine the latter, follow these steps

 $('a').each(function() { var href = $(this).attr('href') || ''; if (href === '') { $(this).attr('href', 'theNewUrl'); } }); 
0
source

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


All Articles