views:

319

answers:

6

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!

+1  A: 

you can get the font-style element and use == operator to get true/false value:

if($(this).attr('font-style') == 'italic')
{
    alert('yes')
}
else
{
    alert('no')
}
Orentet
Thanx for the tip!
Mircea
+4  A: 

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")
}
Ikke
Looks good but it does not work on my code. The italic is added dynamically but I am using live and it should work...<script>$(document).ready(function(){$('#selectable1 span').live('click', function() {if ( !$(this).css('font-style') == "italic") { alert ("yop")}else { alert ("nope")} });});</script>
Mircea
A: 

You want this:

if ( !$(this).attr('font-style') ==  'italic') {
  alert ("yop")
}
else {
  alert ("nope")
}
Nick Craver
+3  A: 

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>
karim79
Thanx Karim. This works!
Mircea
I do see the merit of this approach :) +1 for that.
Mark Schultheiss
Your code works. I went like this and do a small modification:<script>$(document).ready(function(){$('#selectable1 span').live('click', function() {$.fn.hasCss = function(prop, val) { return $(this).css(prop) == val;} if ($(this).hasCss("font-style", "italic")) { alert ("yes") } else { alert ("nop") } });});</script>
Mircea
@Mircea - out of curiosity, I pasted your code change in my edit tool - and wondered if this might be better $(document).ready(function(){ $.fn.hasCss = function(prop, val) { return $(this).css(prop) == val; }; $('#selectable1 span').live('click', function() { if ($(this).hasCss("font-style", "italic")) { alert("yes"); } else { alert("nop"); } });}); allows reuse of plug-in outside the live
Mark Schultheiss
@karim79 - would not toLowerCase be better as toUpperCase?
Mark Schultheiss
@Mark - I've tested your code changes and it works fine. Allowing the reuse of the plugin outside the live is a plus. Thanx
Mircea
+1  A: 

How about this

 if ($(this).css('font-style', 'italic')) {
    alert ("yes")
    }
    else {
    alert ("nop")
    }

But I'd rather use console.log instead of alert if I were you, use firebug its less annoying and more fun :D

<script>

     $(document).ready(function(){ 
         $('#selectable1 span').live('click', function() { 
            if (!($(this).css("font-style","italic"))){ 
                alert ("yop"); 
              } else { 
                alert ("nope"); 
              } 
         }); 
     }); 

</script> 

I don't see why this shouldn't work..

c0mrade
thanx, The code looks good but for some reason does not work for me. i am looking into thisindeed the alert noise makes me bleed...
Mircea
A: 

Try a morf of the conditional and a few semi columns added:

$(document).ready(function()
{
    $('#selectable1 span').live('click', function()
    {
        if ($(this).css('font-style') != "italic")
        {
            alert("yop");
        } 
        else
        {
            alert("nope");
        };
    });
}); 

NOTE: Making assumption regarding the select and subsequent markup based on other comments - would need to see the html to make sure on that.

Mark Schultheiss