0

I have a form_tag form that I want to submit in order to get the values. These same values I want to use in a javascript that is triggered by the same submit button I use for the form.

What I want to do is to first submit the form and then call the javascript.
My submit function looks like that:

<%= submit_tag "Submit", :onclick => "sendRequestToRecipients(); return false;" %>

but it only calls the javascript and doesn't submit the form.

If I do something like this:
:onclick => "submit();sendRequestToRecipients(); return false;"
then I only submit the form but don't call the javascript.

Can I somehow say "trigger this javascript after the form is submitted(using the result of the submission)?

My javascript looks like that:

function sendRequestToRecipients() { { options } , requestCallback); }
2
  • If you submit the form any javascript functionality on the current page is pretty much gone, as it reloads the page. If you don't submit the form, you're not getting the data you're after. Sounds like you need ajax.
    – adeneo
    May 19, 2013 at 14:07
  • I don't know how to do the ajax call, so I guess I will need to go with the page reload. Can I activate the javascript after the page reload, limited to only when the page was reloaded due to the form submission?
    – deyan
    May 19, 2013 at 14:51

1 Answer 1

1

The problem is the return false which prevents the default action of the submit button.

Try:

<%= submit_tag "Submit", :onclick => "sendRequestToRecipients();" %>
1
  • Removing return false lets the form submit (and the page refreshes), but the javascript doesn't load. What I want is the form to submit and the javascript to load right after.
    – deyan
    May 19, 2013 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.