I have a rails application with some javascript. When the user clicks on a div, it triggers a bunch of things and should display / append a new object.

The simplified code is the following :

$(this).append("<div><%= escape_javascript(render(Question.find(15))) %></div>");

The problem is that I can't find anyway to randomly or smartly update the ID of the object called (here 15 for the example).

I can't call a javascript variable because it's inside "escape javascript" and I can't call a rails variable because it would work only once when the page loads, and then always display the same ID when the user triggers the action several times.

Any idea ?

share|improve this question

75% accept rate
1. You should use unobtrusive js 2. You should pass variables through HTML data options 3. You should use ajax call here – fl00r Oct 3 '11 at 13:46
15 should be an id of this or new div or someones else? – Bohdan Oct 3 '11 at 13:51
@fl00r I'll look into this. Though if I make an ajax call to get some data and store it in a javascript variable, will I be able to call it from within escape_javascript(render...) ? – Emmanuel Oct 3 '11 at 13:57
@BohdanPohorilets I'm flexible as I can use a random number or something else, just need it to change at every click – Emmanuel Oct 3 '11 at 13:59
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.