Neil
Neil

Reputation: 5178

jQuery in rails: replace input field with another input type

Within my js.erb file, I successfully grab the input element I want to replace. As a test: I can easily replace that input element with a <p> element:

$(‘.selector_for_input_element’).replaceWith("<p>hello world</p>");

I can even successfully do the same thing with content_tag:

$('.selector_for_input_element').replaceWith("<%= content_tag(:p, "hello world") %>");

However: when I attempt to replace the input element (which happens to be a select box) with a text field: nothing happens:

$('.selector_for_input_element').replaceWith("<%= text_field_tag("name") %>");

With the rails form helpers: how can I successfully swap out the selected input element, and replace it with a text field?

Upvotes: 0

Views: 119

Answers (1)

James Klein
James Klein

Reputation: 612

Moving the comments to an answer. For anyone else looking, the issue is related to string safety in .js.erb.

Flipping quotes to '' on the outside and "" on the inside of the JS statement worked.

Upvotes: 1

Related Questions