3

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?

Rahul Bhargava
  • 528
  • 1
  • 6
  • 17

3 Answers3

6

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).

Community
  • 1
  • 1
jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • 1
    It doesn't need null to be falsy. – Shomz Apr 06 '15 at 11:19
  • Am I really supposed to check the length explicitly? I thought there must be something to avoid that.. I thought. – Rahul Bhargava Apr 06 '15 at 11:20
  • But 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. – jcuenod Apr 06 '15 at 11:21
  • 1
    You can point the OP to [this question](http://stackoverflow.com/questions/17253772/how-does-coffeescripts-existential-operator-work) which quotes the documentation on `?`, explaining why it's not needed here. – raina77ow Apr 06 '15 at 11:29
0

you should either check for "" (blank), because when there's no value entered in textbox, .val() function will return "" .

you can do something like this:

check_fields = ->
  elem_book_title  = ""
  elem_book_author = "asdasd"

  if not elem_book_title? or  elem_book_title == "" 
    alert("title is null")
  else if not elem_book_author? or  elem_book_author == "" 
    alert("author is null")
  else
    alert("both are ok")

Generated JS

var check_fields;

check_fields = function() {
  var elem_book_author, elem_book_title;
  elem_book_title = "";
  elem_book_author = "asdasd";
  if ((elem_book_title == null) || elem_book_title === "") {
    return alert("title is null");
  } else if ((elem_book_author == null) || elem_book_author === "") {
    return alert("author is null");
  } else {
    return alert("both are ok");
  }
};
Mox Shah
  • 2,967
  • 2
  • 26
  • 42
0

tip: when debugging if-not-else-if-not-else I find it is easier to follow code using unless and returning in the block

return alert("title is null") unless elem_book_title
return alert("author is null") unless elem_book_author
alert("both are ok")

which produces similar JS

if (!elem_book_title) {
  return alert("title is null");
}
if (!elem_book_author) {
  return alert("author is null");
}
alert("both are ok");
francpaul
  • 246
  • 1
  • 5