jQuery’s html() function may not work some time

Problem I Faced:

Actually I wanted to render a partial and get it back to the DOM using UJS and html() function of jQuery. But I faced an error  Uncaught SyntaxError: Unexpected token ILLEGAL in browser console. After few hours of research I found that the problem was LF (Line Feed)

<% if params[:request_from].eql? CommonConstants::REQ_FROM_RECRUITERS_DASHBOARD %>
$('#' + <%= params[:id] %>).parents('tr').hide(500, function(){$(this).remove();});
$('#applicants-tabs-1').html('<%= render_applicant_list.gsub("\n", '').squeeze(' ').html_safe %>');

<% else %>
//After the applicant is assigned remove the loading image and show as "Me"
$('#' + <%= params[:id] %>).css("background", "url('') ");
$('#' + <%= params[:id] %>).html("Me");
$('#' + <%= params[:id] %> +'status').html("In-progress");
<% end %>

def render_applicant_list
  render '/recruiters/applicant_list', applicants: current_user.my_applicants, show_current_step: true
end

Solution:

I replaced the Linefeed ‘\n’ with ” (empty string)

Edit

New solution I found:

$('.tab-content').html("<%= j render :partial => 'workflows/applicants_show_custom_steps' %>");
# Escapes carriage returns and single and double quotes for JavaScript segments.
#
# Also available through the alias j(). This is particularly helpful in JavaScript
# responses, like:
#
#   $('some_element').replaceWith('<%=j render 'some/element_template' %>');

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s