Search code examples
javascriptjqueryrepeatclicking

How to click a specific button every second


The webpage's code is as follows :

<div id="content">
    <div class="container-fluid">
        Slots
        <p>The current jackpot is 23220!<p/>
        <p>You lose.</p>
        <form method=post action=/games/slots.php>
            <input type=hidden name='hash_sub' value='MjEzOA=='>
            <input type=hidden name='act' value='spin'>
            <input type=submit value='Spin again!'>
        </form>
        <a href='slots.php'>Go Back</a>
    </div>
</div> 

My code :

setInterval(function solo() {
    jQuery('content').children('submit[value="Spin again!"]').click();
}, 1000);

I am not sure what I am missing, the page loads but the button is not clicked at all. There are no console log errors.


Solution

  • You need to use # with ID and .find() instead of .children(). As .children() only traverses immediate children where as .find() will look into descendants.

    Use

    setInterval(function () {
        jQuery('#content').find('input[type=submit][value="Spin again!"]').click();
    }, 1000);
    

    Also when you use children('submit[value="Spin again!"]') it looks for submit element with value Spin again!.

    DEMO