three3
three3

Reputation: 2846

Check to see if the style attribute exists with jQuery

I am trying to loop through my form inputs to check if the style attribute exists. I feel like I am very close to having the correct code, but something is off. I know there are only 2 inputs that have the style attribute, however, my code is detecting as if all 9 form elements have the style attribute attached.

Here is my code:

$("#form-builder-wrapper input").each(function() {
    var styleAttr = $("#form-builder-wrapper input").attr('style');
    if (typeof styleAttr !== typeof undefined && styleAttr !== false) {
      alert('Has style attribute');
    }
});

Upvotes: 1

Views: 1373

Answers (2)

Anuraag Vaidya
Anuraag Vaidya

Reputation: 847

You can use .is()

$("#form-builder-wrapper input").each(function(index, element) {
    if ($(element).is('[style]')) {
      alert('Has style attribute');
    }
});

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can use the attribute selector to achieve this:

$("#form-builder-wrapper input[style]").each(function() {
    alert('Has style attribute');
});

Upvotes: 2

Related Questions