Getting jQuery to work
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hey guys,

I am working on a Rails App and using Ajax to submit a form (form_for). The form is being submitted correctly and the data items are being saved into the database.

Once the form is submitted, using AJAX I render a new form. In this form, I need to update a select tag with the entry for the last item submitted in the previous form.

Here is my jQuery code: $('.guest_box select#tag_event_id').append(new Option(<%= j Event.last.name%>, <%= j Event.last.id.to_s %>));

name is a string and id is an integer. I am making some mistake in passing the Rails variable Event.last.name and Event.last.id because when I try this jQuery in the console with hardcoded values it work correctly.

Any ideas what mistake I am making?

awarded to Wikimedia via magicmarkker

Crowdsource coding tasks.

1 Solution

Winning solution

var option = $('<option/>');
option.attr({ 'value': '<%= j Event.last.id.to_s %>' }).text('<%= j Event.last.name%>');
$('.guest_box select#tag_event_id').append(option);