Question

I have a rails form with a submit button, but I'm wondering if it's possible to make this submit button 1) Post to controller create action and also 2) Execute Javascript

Here are the two buttons I essentially want to combine:

1. <%= f.submit "Go!", class: "btn btn-primary btn-large btn-block" %>

2. <a href="javascript:ExtInstall()" class="btn btn-default btn-large btn-block" role="button">Download our Google Chrome extension</a>
Was it helpful?

Solution 2

If your button is part of a form, you can keep its functionality the same as it is now. If it's stand-alone, you'll be best using button_to like so:

<%= button_to "Download", post_path, id: "button" %> # -> uses POST http verb, so will send to create action

What you're asking is best answered with unobtrusive javascript. This will bind your button's click event to a function of your choice, allowing you to not only submit the required data, but handle the other functionality you need too:

#app/assets/javascripts/application.js
$("#button").on("click", function(){
    ExtInstall(); //do you have any references for this function?
});

OTHER TIPS

Yes, you can do this by adding an onClick handler in JS to the submit button.

You can see submit_tag with javascript function for the different ways you can do this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top