javascriptjqueryjquery-eventsuserscriptsvbulletin

How to select a radio then submit form with jQuery in vBulletin?


I am working on a userscript that injects my own JS into the head (this part works). I am utilizing the site's already in-place jQuery.

I am a moderator on a vBulletin forum. I move and close A LOT of threads. I figured it would save me insurmountable amount of time if I reduced the total number of clicks I make by 2 clicks per thread.

Using .append() I've added my own <td> with a "quick access" <a> tag.
What I would like to happen is for when I click the <a> tag (Close or Move) it selects the appropriate option in the thread tools form (which as far as I can tell is working the way I have it coded) and then submit the form as if I had hit the Perform Actions button.

My current code is:

$(document).ready( function() {

    var topbar = $('#threadtools').closest('tr');
    var newTool = '<td><a href="#" id="newClose" style="padding-left: 10px; padding-right: 10px;"><b>Close</b></a></td>' +
                    '<td><a href="http://google.com" id="newMove"><b>Move</b></a></td>'; // Google for testing purposes I wanted to make sure preventDefault was working
    topbar.append(newTool);

    $('#newClose').click(function() { 
        alert();
    });
    $('#newMove').click(function(event) { 
        event.preventDefault();
        //var form = $('input[name$="threadadminform"]');  // select form by name
        var button = $('input[value="Perform Action"]');   // select button by name


        var closeradio = $('#ao_oct');
        var moveradio = $('#ao_mvt');
        var copyradio = $('#ao_cpt');
        var editradio = $('#ao_edt');
        var deleteradio = $('#ao_dlt');
        var stickradio = $('#ao_sut');
        var mergeradio = $('#ao_mgt');
        var redirectradio = $('#ao_rrd');

        closeradio.removeAttr('checked');
        copyradio.removeAttr('checked');
        editradio.removeAttr('checked');
        deleteradio.removeAttr('checked');
        stickradio.removeAttr('checked');
        mergeradio.removeAttr('checked');
        redirectradio.removeAttr('checked');

        moveradio.attr('checked', 'checked');
        button.trigger();
    });
});

As you can see I am currently trying to .trigger() the submit button, this does not work.
I have also tried .submit() on the form var I declared at the top, this also does not work.

How can I get this to submit as it would if I clicked the button and take me to the next appropriate page?


Solution

  • trigger needs a minimum of event, so you need to pass button.trigger('click') so that it will be able to perform the click event.

    Sample demo of what you are trying to achieve is at, take a look at two button example at the above location.

    hope this helps.