0

I cant help but feel stupid. I have a simple setup which should be working but is not. Obviously im missing something simple. Im trying to get it to load on doc ready rather than using window scroll btw.

why does this not work?

if ($(".container").css("width") == "100%"){
    alert("adfgf adg df afdgadg");
}

http://jsfiddle.net/bxLLH/2/

thanks in advance.

2 Answers 2

1

use width():

if ($(".container").width() == $(window).width()){
    alert("adfgf adg df afdgadg");
}

demo: http://jsfiddle.net/bxLLH/3/

Sign up to request clarification or add additional context in comments.

1 Comment

I didnt want to use window width since js / media query pixels are a bit off but I guess since its 100% of both it would be ok in this instance. Thanks I didnt think to do it this way
1

jQuery seems to only return pixel widths rather than percentages. Try using

if ($('.container').width() === $('.container').parent().width())

That will check to be sure something is the same width as it's direct parent element.

3 Comments

no friggin way, lol. So how is one supposed to do jquery checks on responsive layouts that use percentages? Is there a way to do it w/ jquery ui possibly?
If the parent element is 500px wide and the child is 100%, well then the child element is 500px! If you drag it smaller and the outer element is now 250px, then the inner, that is still set to 100% will return 250px. Try it out if you don't believe me, but you came looking for an answer and you got it.
It wasnt a matter of believing you, I believe you because I tried both your answers and they work. I am just blown away that jquery doesnt calculate percentages. I guess I just have to check other parts of the css that media queries are changing other than percentages...Thanks for the answer!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.