Reputation:
I have this coffeescript code
greet = ->
if $('#output').html() is 'Hi' and $('#textName').val() is not ""
$("#output").append(" how are you #{$('#textName').val()}?")
The second condition is the value of an HTML input. If I quit the second condition the script work but if I put it (as in the code above) the script doesn't work no matter what.
I tested the value of the input through some alert commands and the value is actualized so the problem is not the value itself if not about how the boolean or the function works, by example if I enter in the input something like "Markus" and run this code
greet = ->
if $('#output').html() is 'Hi' and $('#textName').val() is not ""
$("#output").append(" how are you #{$('#textName').val()}?")
alert $('#textName').val()
The alert shows "Markus" but the function still doesn't work, the .append fail to add text to a div. The HTML involved is something like this
<body>
<form>
<fieldset>
<input placeholder="Put your name" id="textName" type="text">
</fieldset>
</form>
<div id="parent">
<div id="output" onmouseover="greet()">Hi</div>
</div>
</body>
Can you help me please? Thank you in advance.
P.S.: I verified the correctness of the coffeescript code in the compiled javascript. I used parenthesis to encapsulate both boolean conditions too but doesn't work either.
Upvotes: 0
Views: 74
Reputation: 82
I think your coffee syntax is error.
In CoffeeScript
is
=> ===
not
=> !
$('#textName').val() is not ""
=> $('#textName') === !""
So, you never pass the condition. because !"" === true
Upvotes: 0
Reputation: 434795
a is not b
does not do what you think it does. If you look at the JavaScript version of:
if $('#textName').val() is not ""
you'll see
if ($('#textName').val() === !"")
and !''
is just a strange way of saying true
so your conditional will end up comparing a string (or undefined
) to a boolean and that will never be true.
The opposite of is
(AKA ==
) is isnt
(AKA !=
); is not
is two operators, not one.
If you're sure that $('#textName').val()
will always give you a string, then you want to say:
# ------------------------------------------------------vvvv
if $('#output').html() is 'Hi' and $('#textName').val() isnt ""
or you could just say:
if $('#output').html() is 'Hi' and $('#textName').val()
because a non-empty string is still truthy in CoffeeScript just like it is in JavaScript.
Upvotes: 0