Data attributes in jQuery / Coffee script

I have the following form code:

<input data-color="orange" id="vote_color_id_2" name="color" type="radio" value="2" />Orange <input data-color="blue" id="vote_color_id_3" name="color" type="radio" value="3" />Blue <input data-color="green" id="vote_color_id_4" name="color" type="radio" value="4" />Green 

I am using Coffee script in rails, and now I'm just trying to warn the value of the data-color data attribute.

This is my coffee script.

 jQuery -> $("input[name='color']").change -> color = this.data() alert color.color 

Compiled jQuery looks like this:

 (function() { jQuery(function() { return $("input[name='color']").change(function() { var color; color = this.data(); return alert(color.color); }); }); }).call(this); 

But do I keep getting this error?

 Uncaught TypeError: Object #<HTMLInputElement> has no method 'data' 
+8
jquery coffeescript
source share
1 answer
 jQuery -> $("input[name='color']").change -> color = $(this).data() alert color.color 

jQuery assigns this element that called the callback, but is never a jQuery object. So you need to wrap it if you want to call jQuery methods on it.

+24
source share

All Articles