0

This shouldn't be a difficult problem to answer, but I can't find my answer from searching google.

I'm trying to have a form that tells me how many characters you have remaining. I got the code working in basic html, but I'm having difficulty converting it into rails.

In html, the relevent piece of code looks like:

<textarea name="content" onKeyUp="rem_char(this.form.content);"></textarea>

In Rails, the code looks like:

<%= f.text_area :content, :onkeyup => "rem_char(this.form.content);" %>

I can tell from viewing the source of the Rails document that the name of textarea is "micropost[content]", but changing "this.form.micropost[content]" does not work. In addition, adding :name => "content", causes the form input to not be read.

1 Answer 1

1
<%= f.text_area :content, :onkeyup => "rem_char(this);" %>

When that JS runs, this is the textarea. You then ask for the textarea's form, then ask the form for the element named content, which is the textarea. So save yourself the name dependent roundtrip there entirely, and just use this.


Alternatively, use id, not name.

<%= f.text_area :content, 
      :onkeyup => "rem_char(document.getElementById('micropost_content'));" %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.