>> That would still be a bad way to do it i'd say, because if theres more
>> than one element with the id 'warningmsg' ... it would fire more than
once.
>
> Not true. If you have multiple elements with the same id and
> you use id selection syntax (#), your result set will contain a
> single element (the first one).
I think IE (c'mon, do I even need to *finish* this sentence?) returns an
array in those cases. However I'm not sure jQuery handles that case.
Basically, a document with two identical ids is not well formed and the
browser is free to behave however it wants because it's never supposed to
get there.
Peter's original code looked like this:
> if ($('#warningmsg').size()) highlightWarningMsg();
Which should work fine. So would $('#warningmsg').length which would be my
preference for getting the number of elements. It's only half the puzzle
though, we don't know how #warningmsg gets there or what highlightWarningMsg
does. If it's put into the document on the server side then the function may
start an animation of some sort. Or maybe it just sets some style elements
like color:red, which could be done directly like
$('#warningmsg').css({color: "red"}) rather than calling a function at all.
All that said, jQuery tries to encourage a functional programming paradigm
with its design. An "if" is procedural. I just wanted to point out
functional ways to do the same thing that fit better with jQuery's style.
Peter's original code should work fine, but it sounded like he was looking
for other ways to achieve the goal. jQuery gives you plenty of other
options.
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/