I was trying to figure out why my CoffeeScript code was not working:
HTML:
<a data-id="5">Click me</a>
CoffeeScript:
id = $('a').attr('data-id')
console.log id == 5
The problem is the number being returned from attr('data-id') is a string and comparing it to an actual number fails the comparison. What should I be doing differently so that I can easily compare numbers.
You may use parseFloat or parseInt methods, and also take a look at jQuery data method which tries to convert content of
data-attribute to appropriate data type