if else in coffee script
In jQuery, .val()
won't return null for an empty field (except for a select element). Right now your coffee script evaluates to:
if (elem_book_title == null) {
return alert("title is null");
} else if (elem_book_author == null) {
return alert("author is null");
} else {
return alert("both are ok");
}
So try removing the question marks
if not elem_book_title
alert("title is null")
else if not elem_book_author
alert("author is null")
else
alert("both are ok")
This will produce the js I think you were expecting (which is testing for falsiness like an empty string, 0 or null
):
if (!elem_book_title) {
return alert("title is null");
} else if (!elem_book_author) {
return alert("author is null");
} else {
return alert("both are ok");
}
There is a question about the way coffeescript's existential operator (?
) works here that you may find informative (thanks @raina77ow).
Rahul Bhargava
Updated on June 27, 2022Comments
-
Rahul Bhargava almost 2 years
Folks,
I have two text boxes in my html. I wanted to compare their values using coffee script. Although I have googled about it, and I am kind of sure that I am doing what is expected to do but still I see a strange behavior.
Thing is :
Lets say, I have two text boxes with id as "title" and "author". Along with that, I have a button which onclick triggers the coffee script function.
My coffee script function looks as:
check_fields = -> elem_book_title = $("#title").val() elem_book_author = $("#author").val() alert(elem_book_title) alert(elem_book_author) if not elem_book_title? alert("title is null") else if not elem_book_author? alert("author is null") else alert("both are ok")
Situation is, If I enter something only in my "title" textbox, it should alert me that "author is null". Right? But surprisingly, it alerts me that "both are ok".. Expected? Or I missed something?
-
Shomz about 9 yearsShouldn't you check for values and not the elements?
-
Mox Shah about 9 yearswhat does it alert when you do
alert(elem_book_title.val())
? -
Rahul Bhargava about 9 years@Shomz, Well. Yes, I tried that too.. Something like:
elem_book_title = $("#title").val()
elem_book_author = $("#author").val()
alert(elem_book_title)
alert(elem_book_author)
Rest code is same. But of no use. -
Rahul Bhargava about 9 years@MokshShah, It alerts the same.
-
Rahul Bhargava about 9 yearsSorry for editing the post many times. I was not getting the
code
right. -
raina77ow about 9 yearsjQuery constructor function always returns an object - that is a truthy value. You should rewrite your code so that the values of those inputs are checked.
-
Rahul Bhargava about 9 years@raina77ow.. Please see my above comment.. I have tried .val as well.
-
raina77ow about 9 yearsAhem. It's not 'as well'. It's the only proper way to do it. It doesn't guarantee that the rest of your code doesn't have an error, yes. But if you don't fix 'using the objects in checks' error, it's meaningless to advance further.
-
Rahul Bhargava about 9 years@raina77ow.. Ahem. I fixed that and it still alerts the same "both are ok". What now?
-
raina77ow about 9 yearsCould you show how your code looks like after the fix, editing the question accordingly?
-
Rahul Bhargava about 9 years@raina77ow.. I have edited the code in question itself. Please have a look.
-
raina77ow about 9 yearsNow the answer given by @jcuenod fits your question precisely. And any other visitor of this page won't have to check the comments to understand what's wrong. That was my point actually.
-
-
Shomz about 9 yearsIt doesn't need null to be falsy.
-
Rahul Bhargava about 9 yearsAm I really supposed to check the length explicitly? I thought there must be something to avoid that.. I thought.
-
jcuenod about 9 yearsBut look at what your coffeescript evaluates to:
if (elem_book_title == null)
. If you remove the question marks then it will test falsiness... I've updated my answer. -
raina77ow about 9 yearsYou can point the OP to this question which quotes the documentation on
?
, explaining why it's not needed here. -
mu is too short about 9 yearsBut an empty string is still falsey (as are
null
andundefined
) in CoffeeScript so!elem_book_title
sufficient.