jeffimperial
jeffimperial

Reputation: 73

how to remove attribute from link using jquery

I have a link that looks like this:

<a onclick="myLightbox" rel="lightbox" href="image.jpg" title="">

I am trying $element.prop("onclick", null); to remove the attributes onclick and rel, but just don't know how to make it work

Can anyone please help me? The a tag does not have an id, and I just can't make it have one as that link is generated by a CMS i do not have control over.

Upvotes: 0

Views: 2922

Answers (5)

Jignesh Rajput
Jignesh Rajput

Reputation: 3558

use removeAttr

   $("a[href*='image.jpg']").removeAttr("rel");

Demo : http://jsfiddle.net/eFqsG/3/

Upvotes: 1

evilunix
evilunix

Reputation: 960

$('a').each(function(index, a) {
    var a = $(a);
    if(a.attr('rel') == 'lightbox' && a.attr('onclick') == 'myLightbox') {
        a.attr('rel', null).attr('onclick', null);
    }
});

Upvotes: 0

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

Put an id or class for jquery selection or you can also select with tag like this

$('a').prop("onclick",null);

You must provide selector as $element is not recognized here.

Upvotes: 0

David Hedlund
David Hedlund

Reputation: 129792

  • rel is not a property but an attribute. Use .removeAttr('rel')
  • For onclick, see unbind.

Upvotes: 0

Jonas Stensved
Jonas Stensved

Reputation: 15286

Use removeAttr.

Upvotes: 1

Related Questions