if ($("#makespan").is(":visible") == true) {
var make = $("#make").val();
}
else {
var make = $("#othermake").val();
}
Make:<span id=makespan><select id=make></select><span id=othermakebutton class=txtbutton>Other?</span></span><span id=othermakespan style="display: none;"><input type=text name=othermake id=othermake> - <span id=othermakecancel class=txtbutton>Cancel</span></span>
The above code runs smooth in Firefox, but doesn’t seem to work in Chrome. In Chrome it shows .is(":visible") = false even when it is true.
I am using following jQuery version: jquery-1.4.3.min.js
jsFiddle Link: http://jsfiddle.net/WJU2r/4/
It seems jQuery’s
:visibleselector does not work for some inline elements in Chrome. The solution is to add a display style, like"block"or"inline-block"to make it work.Also note that jQuery has a somewhat different definition of what is visible than many developers:
In other words, an element must have a non-zero width and height to consume space and be visible.
On the other hand, even if its
visibilityis set tohiddenor the opacity is zero, it’s still:visibleto jQuery as it consumes space, which can be confusing when the CSS explicitly says its visibility is hidden.The easy way to look at it, is that if you can see the element on the screen, even if you can’t see its content, it’s transparent etc., it’s visible, i.e. it takes up space.
I cleaned up your markup a little and added a display style (i.e. setting the elements display to “block” etc), and this works for me:
FIDDLE
Official API reference for
:visibleAs of jQuery 3, the definition of
:visiblehas changed slightly