> From: Pops
>
> I think I have a "theory" about why this is happen and it
> might not be jQuery related but just Javascript, but maybe
> someone can explain this because it is so odd.
>
> This is the code:
>
> logln("wcError "+IsHidden('wcError')?"hidden":"not hidden");
>
> logln() is a function tha basically appends text to a log window.
> However, I don't see
>
> "wcError hidden" or "wcError not hidden"
>
> but rather:
>
> " hidden" or "not hidden"
>
> in other words, the left side string "wcError " is not concentated.
Hector, the + operator has higher precedence than the ?: operator. So your
code is equivalent to:
logln( ( "wcError "+IsHidden('wcError') ) ? "hidden" : "not hidden" );
That should always display "hidden", since the test expression will always
evaluate to a "true" value.
> In the same vain, I now see it is similar to a fix I had to
> make in another project where I had a table paging logic:
>
> function $(v) { return(document.getElementById(v)); }
> function gotoPrevPage() {
> $("start").value -= 1*$("rows").value; // <- WORKS AS EXPECTED
> }
>
> function gotoNextPageBUG()
> {
> $("start").value += 1*$("rows").value; // <- BUG!!
> }
>
> function gotoNextPageFIX()
> {
> $("start").value -= -1*$("rows").value; // <- FIX!!
> }
>
> I was multiplying by 1 to help JS type cast the operaton. But that
> didn't work went using the inclusive += operatoin. Worked fine with
> -= operation. So I used the reverse logic to multiply by -1 instead
> with -=.
>
> To me, that looks look like a JS bug? No?
No. :-)
$("start").value is a string. += can be either a numeric operator or a
string operator. In this case it is a string operator, just as if you'd
written:
$("start").value = $("start").value + 1*$("rows").value; // <- BUG!!
OTOH, -= is always a numeric operator, just like - is.
Try these examples in the Firebug console:
"10" + 2
"10" - 2
In the first example, the 2 is converted to "2" and then concatenated to
"10", for a result of "102".
In the second example, the "10" is converted to 10 and then 2 is subtraced
from it, for a result of 8.
One good way to write your function would be:
function gotoNextPageMikesWay()
{
var start = $("start");
start.value = +start.value + ( +$("rows").value );
}
The unary + converts each value to a number. You'll see this fairly often;
it basically does the same thing as your 1* trick. The extra parentheses are
there to make sure the code still works if it's run through a compressor. If
you just coded:
start.value = +start.value + +$("rows").value;
It would work as long as that space is there, but would fail if the + + get
squished down to ++.
Or your 1* trick would work just as well:
start.value = 1*start.value + 1*$("rows").value;
One thing that can help with stuff like this is to break out the DOM vs.
core JavaScript questions. In this code, the problem revolves around the
type of element.value. So you could see what it is with this in the Firebug
console:
$("start").value // displays a value enclosed in quotes - it's a string!
typeof $("start").value // let's confirm that - it displays "string"
Then you can experiment with tests like the "10" + 2 that I mentioned above.
-Mike