Hassan Munir Hassan Munir - 4 months ago 11
Ajax Question

Want to add dynamic list with dynamic data through js

There is a main file in which i want to add li in ul with dynamic value.

<ul class="selected_questions"></ul>


In my js.erb i want to show title. I have debugged it and @ques value is there but not showing in ul. Following is my code.

<% if @ques.errors.any? %>

console.log('Error');
$('#dialog-form').html('<%= escape_javascript(render('form')) %>');
<% else %>
console.log('Created');
$(".selected_questions").append('<li><span class="tab">"'+@ques.title+'"</span></li>');
$('#dialog-form').dialog('close');
$('#dialog-form').remove();
<% end %>


I also have tried following way :

$('<li />', {html: @ques.title}).appendTo('ul.selected_questions')


Simple string is appended successfully but object from controller not been shown. What am i doing wrong ???

Answer

Try this

change this

 $("#selected_questions").append('<li><span class="tab">"'+@ques.title+'"</span></li>');

to

$("#selected_questions").append('<li><span class="tab"><%= @ques.title %></span></li>');