ɐlǝx
ɐlǝx

Reputation: 1394

JQuery - Modify the attributes of a subset of a class


supposing I have to modify the attributes of a subset of a class (same class name), for example, only those that have an indefinite href.
I tried this

if(jQuery('.aaa').attr('href') === "undefined")
{ 
   jQuery(this).css( "cursor", "default" );  
   jQuery(this).removeAttr('href');
}

I also tried this, but it doesn't work

jQuery( ".aaa" ).filter(function( index ) {
    return jQuery( ".aaa", this ).attr('href') === "undefined";
  }).css( "cursor", "default" );

Any tips? Thanks.

Upvotes: 0

Views: 36

Answers (2)

Daniel Beck
Daniel Beck

Reputation: 21485

I'm pretty sure the only way for href to be undefined is for the attribute to be absent. If you're looking for empty or bare href attributes, test for the empty string instead:

// classnames so you can see what matches what
$('.aaa:not([href])').addClass('undefined');
$('.aaa[href=""]').addClass('empty');
.undefined{background-color:red}
.empty {background-color:blue}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a class="aaa">No href</a>
<a class="aaa" href="">Empty href</a>
<a class="aaa" href="#">Non-empty href</a>
<a class="aaa" href>Bare href</a>

Upvotes: 0

Satpal
Satpal

Reputation: 133403

Assuming you want to modify the css of anchor whose href is undefined

jQuery(".aaa").filter(function(index) {
    //return jQuery(this).attr('href') === "undefined";
    return this.href === undefined;
}).css("cursor", "default");

Upvotes: 1

Related Questions