Search code examples
javascriptprototypejs

comparing strings in javascript


I am comparing two strings that supposed to be equal. From some reason it thinks they are not.

this is the code:

   function(){
    prev = $('h').value;
    now = $('content').innerHTML;
    alert(prev);
    alert(now);
    if(prev == now)
    {
    $('content').fade({ duration: 3.0, from: 0, to: 1 });
    }
    else{alert('lol');}
    }

I made added the alert functions to be sure they are equal.

Any idea why it gives me the "alert('lol)" ?


Solution

  • Your strings are probably different, but it's hard to see leading and trailing whitespace in alert() boxes. You can try to trim() the strings:

    if ($.trim(prev) == $.trim(now)) {
        $('content').fade({ duration: 3.0, from: 0, to: 1 });
    }