MegaXLR MegaXLR - 1 month ago 6
CoffeeScript Question

Selecting element its value in coffeescript

I want to select an element based on its name and get its value.

I currently have:

document.getElementsByName 'employee_search_first_name'[0].value


which compiles to:

document.getElementsByName('employee_search_first_name'[0].value)


But I need:

document.getElementsByName('employee_search_first_name')[0].value


To make my situation a bit clearer:


  • I do not have access to jQuery or any framework for that matter.

  • I do not want to store the element in a variable as I need more than just this one.



What would be the valid coffeescript to get this?

Answer

Parentheses are optional in CoffeeScript, unless they're not. This is a case where you need the parentheses to disambiguate:

document.getElementsByName('employee_search_first_name')[0].value

Yes, this is valid CoffeeScript.

Comments