Search code examples
javascriptjqueryuserscripts

Input type submit with multiple functions in Javascript


I recently discovered the use of userscripts and since my work has to do with repetitive tasks on a website, I needed to create a button that would hold multiple functionality. The code I have so far is creating the button and applies the function doAll which is selecting the two radio buttons. After executing this I also want the button to have the ability to submit. How can I implement it? Thanks in advance.

var input=document.createElement("input");
input.type="button";
input.value="Save";
input.onclick = doAll;
document.body.appendChild(input); 


function doAll() {
    $('[id^=radio1_]').prop("checked", true)
    $('[id^=radio2_]').prop("checked", true)
    }

Solution

  • Use a selector to find the form with the action you want:

    function doAll() {
        $('[id^=radio1_], [id^=radio2_]').prop("checked", true);
        $('form[action="/photo/edit?id=1234"]').submit();
    }