Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript on page is not executing before AJAX onComplete event is called
    text
    copied!<p>I have a form that makes an Ajax POST request to insert a widget into my database. In the form, I have a select box where you can select from the widgets. After the db insert is made, I must update the select box. I actually just replace the entire form for now. </p> <p>Because the select box has the widgets, I must have a copy of the objects in javascript. I call this var widget_objects. When the form is replaced during the update event, I print the ruby variable &lt;%= @widget_objects %> and I can see the newly created object. However, when I try to access the javascript var "widget_objects" in the onComplete event, the new object does not exist. I create the javascript widget_objects with this line of code on the page: widget_objects = &lt;%= @widget_objects %>;</p> <p>So it seems that the line of code above is not executed before Ajax request's onComplete event. However, I thought the onComplete event occurs after the page has been loaded, and I would assume after scripts are eval'd....any ideas?</p> <pre><code>&lt;%= submit_to_remote( "save_widget", "Save Widget &amp; Generate Embed Code", { :url =&gt; widgets_url(:user_id =&gt; @user.id), :update =&gt; "widget_form", :method =&gt; :POST, :html =&gt; { :id =&gt; "save_widget_button", :onclick =&gt; "this.value='Saving...'; this.disabled = 'true';", :style =&gt; "width: 220px;" }, :complete =&gt;" $('save_widget_button').disabled=''; $('save_widget_button').value='Save Widget &amp; Generate Embed Code'; var last_id = $j('select#widget_id').children(':last').attr('value'); alert( widget_objects[last_id] ); ", :success =&gt; "reportMessage('success', request.headerJSON.success, 'save_widget_status'); $('band_form').reset();", :failure =&gt; "reportMessage('failure', request.headerJSON.errors, 'save_widget_status');" }) %&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload