Jason S. Burton
Jason S. Burton

Reputation: 127

JQuery Selector Question -- How to find all HREF's with a target = _blank?

My "JQuery Selector Foo" stinks. I need to find all HREF's with a target attr of _blank and replace them with a common window/target. Assistance is greatly appreciated!

Upvotes: 9

Views: 13830

Answers (3)

JaredPar
JaredPar

Reputation: 754735

If you're specifically looking for href values which have blank values then do the following

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

This will catch only anchor tags which have a href attribute that is empty. It won't work though for anchors lacking an href tag

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

If you need to match the latter then do the following

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

Upvotes: 0

amiry jd
amiry jd

Reputation: 27585

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 do you want, for more help

Upvotes: 2

Ry-
Ry-

Reputation: 224913

$("a[target='_blank']").attr('target', 'sometarget');

Do you mean something like that?

Upvotes: 19

Related Questions