1) If you want to perform certain operation as a first activity after the save button click, then you would have achieved it via server side script (here PHP) by hooking into some action, but as you are clearly specifying that you want client side action only, I think there is no straightforward way to control the order of the even like click
. However, one following can be done -
If you have to perform more than one JavaScript operations, then you can put your functions in the order accordingly.
e.g.
on click, func1()
should execute first and then func2()
, so just call these in that order.
2) If you have to perform only one JavaScript operation, then it will anyway be executed and then server side code will execute on submit.
OR if this approach doesn't completely execute your JS function first, then you can use preventDefault()
to let your JS function execute first and then automatically submit the form using JavaScript function only
e.g. .submit()
is available in jQuery
3) Apart from these, I have one more way which I'm not completely sure but may help you -
Can you use onfocus
event instead? This will execute before click and will work with mouse as well as keyboard.
But, a disadvantage is that it will always execute your script irrespective of whether the user actually submitted the form or not.
4) WordPress widget saves the widget form via ajax, so you can check if there is any hook available where you can inject your script and get your script executed before the form entry is actually made in database.
mousedown
event on the submit button.widget-updated
make.wordpress.org/core/2014/04/17/…