Julian Dormon
Julian Dormon

Reputation: 1779

Jquery - find links that have no target or a certain target

I'd like to find all the links on a page that either have no target attribute or a target attribute that equals 'pageContent' and for all matched elements I would like to apply an onclick event to them.

The onclick event is called pgTrans('start'). I'm not sure if it matters but some of the links may already have this event hard coded or attached via jQuery.

Upvotes: 2

Views: 2669

Answers (2)

Sotiris
Sotiris

Reputation: 40046

Try the following:

$('a:not([target]), a[target="pageContent"]').click(function(e) {
    e.preventDefault();
    pgTrans('start');
});

Demo: http://jsfiddle.net/UpjmU/

using :not() selector and inside [target], selects all a that don't have a target attribute. In the second case, using [target="pageContent"], selects all elements that the attribute target equals pageContent

Upvotes: 7

Paul
Paul

Reputation: 141829

The function will be called twice by any links that already have an event handler to do this attached.

$('a[target=""], a[target="pageContent"]').click(function(){
        pgTrans('start');
    }
);

Upvotes: 1

Related Questions