Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery check if element have specific attribute

I am trying to see if an element have a specific CSS attribute. I am thinking at something like this:

if ( !$(this).attr('font-style', 'italic')) {
alert ("yop")
}
else {
alert ("nope")
}

It does not work. Any tips on this? Thank you!

like image 999
Mircea Avatar asked Dec 22 '22 05:12

Mircea


2 Answers

Here's a simple plugin to do that (tested):

$.fn.hasCss = function(prop, val) {
    return $(this).css(prop.toLowerCase()) == val.toLowerCase();
}

$(document).ready(function() {
    alert($("a").hasCss("Font-Style", "Italic"));
});

<a style="fonT-sTyle:ItAlIc">Test</a>
like image 199
karim79 Avatar answered Feb 10 '23 07:02

karim79


You are trying to get a css style. The css method can get the information

if ( !$(this).css('font-style') == "italic") {
    alert ("yop")
}
else {
    alert ("nope")
}
like image 32
Ikke Avatar answered Feb 10 '23 08:02

Ikke