marcamillion marcamillion - 5 months ago 22
Javascript Question

How do I access the value of an HTML attribute from within CoffeeScript?

I am using the summernote WYSIWYG editor along with SimpleForm.

So this form field:

<%= f.input :description, as: :summernote, placeholder: "Enter the description of the Job (e.g. 'Product Manager', 'Senior Ruby on Rails Developer')" %>


Generates the following HTML:

<textarea class="summernote optional" placeholder="Enter the description of the Job (e.g. 'Product Manager', 'Senior Ruby on Rails Developer')" data-provider="summernote" name="job[description]" id="job_description" style="display: none;"></textarea>


My SummerNote JS is executed as follows:

ready = ->
$('[data-provider="summernote"]').each ->
$(this).summernote(height: 300, toolbar: [['style', ['bold', 'italic', 'underline', 'clear', 'fontname', 'fontsize', 'color']],
['font', ['strikethrough', 'superscript', 'subscript']], ['para', ['ul', 'ol', 'paragraph']],
['height', ['height']], ['insert', ['link', 'table', 'hr']]], placeholder: "Some placeholder copy")

$(document).ready(ready)
$(document).on('turbolinks:load', ready)


However, instead of the
Some placeholder copy
, I would like to pull the value that is specified in the form (i.e. the placeholder in the
<textarea>
field).

How do I do that?

Answer
$(this).attr "placeholder" # => the attribute value

It's basic jQuery. this is the textarea, and .attr is the jQuery method for reading/writing attribute values.