Excellent, Dave. Your explanation is much appreciated. I'd tried something
similar in my 'period of frustration', but didn't know about the []'s
denoting descendents.
dave.methvin wrote:
What I'm trying to achieve is the element with id=save-search only
gets hidden if it does not contain
#save-search// select an element with id=save-search
:not( // as long as the following is not found:
[ // descendent elements containing
div // a div
.error-message// with class
Hi
I thought I was getting the hang of JQuery until I tried to optionally not
select an element
What I'm trying to achieve is the element with id=save-search only gets
hidden if it does not contain a div with the class=error-message
This is what I thought it should be, but it doesn't have
jQuery objects are collections of objects and quack* (act) like
Array objects. So you could do this:
$(document).ready(function() {
if ( $(#save-search div.error-message).length == 0 )
$(#save-search).hide();
});
Karl Rudd
* A slightly obscure reference to Duck Typing -
$(document).ready(function() {
if (!$(#save-search div.error-message)) $(#save-search).hide();
});
Would it be possible to let me know not just how to get this to work, but
also where I'm going wrong?
Your test will never fail because $(..) always returns an object which
does not resolve
Thank you Karl - much appreciated the explanation. I (wrongly) assumed a
select of nothing would return false or null, not an empty quacking array...
Karl Rudd wrote:
jQuery objects are collections of objects and quack* (act) like
Array objects. So you could do this:
Thanks Mike. Have to admit to a fatal flaw in my logic (that a nothing select
would return nothing), but I'm still *extremely* impressed with JQuery (just
not with myself)
malsup wrote:
$(document).ready(function() {
if (!$(#save-search div.error-message)) $(#save-search).hide();
});
What I'm trying to achieve is the element with id=save-search only
gets hidden if it does not contain a div with the class=error-message
How about this?
$(#save-search:not([div.error-message])).hide();
It actually reads very much like the sentence above, once you know what all
the syntax