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>, <%= j %>));

name is a string and id is an integer. I am making some mistake in passing the Rails variable and 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 %>' }).text('<%= j>');
$('.guest_box select#tag_event_id').append(option);