Canna
Canna

Reputation: 3794

How can i append rails form_helper tag to html

I have to append the rails select_tag code after the click event,

this is the code,

var result = '<%=select_tag("repeat_time", options_for_select(get_repeat_time_list_daily, "1")) %>';
$("#repeat_time_area").html(result);

and it has this error,

Uncaught SyntaxError: Unexpected token < 

and this is the html parsed code,

var result = '<select id="repeat_time" name="repeat_time"><option value="1" selected="selected">1일</option>
<option value="2">2일</option>
<option value="3">3일</option>
<option value="4">4일</option>
</select>'

as you can see, rails code automatically makes a new line and that cause the problem.

var result = '<select id="repeat_time" name="repeat_time"><option value="1" selected="selected">1일</option><option value="2">2일</option><option value="3">3일</option> <option value="4">4일</option></select>'

this is my expected way.

I tried all kind of things to make this as a straight string line without new line

.gsub, h, html_safe, gstrip

but all this kind of libs are not working :(

any good solution??

Upvotes: 1

Views: 92

Answers (1)

vee
vee

Reputation: 38645

Use escape_javascript or alias j() helper to escape carriage returns, single and double quotes for JS arguments as:

var result = '<%= j select_tag("repeat_time", options_for_select(get_repeat_time_list_daily, "1")) %>';
$("#repeat_time_area").html(result);

Upvotes: 1

Related Questions