Ryan Cooper
Ryan Cooper

Reputation: 703

jQuery click button only once

I'm almost completely clueless when it comes to Javascript... However I'm simply trying to tweak a userscript i use that implements jQuery to get a element by its ID and automaticlly click it... Problem is it keeps clicking it and goes into a loop. I've researched methods to prevent this such as .bind/.unbind and .one but i am unsure of how to implement them? Any help would be appreciated.

$(document).ready(function() {
 document.getElementById('submitGiftCardContinueBtn').click();
});

Upvotes: 2

Views: 5565

Answers (2)

Jakub Arnold
Jakub Arnold

Reputation: 87220

Since you're already using jQuery, I'd recommend writing it as

$(function() {
    $("#submitGiftCardContinueBtn").click().attr("disabled", "disabled");
});

since document.getElementById() isn't very jQuery-ish :)

Upvotes: 2

u.k
u.k

Reputation: 3091

I'm wasn't able to reproduce this. However, disabling the button right after clicking should be a good practice regardless, Since (due to network latency, code efficiency, etc) there could be a lag between the click event and the actual action.

$(document).ready(function() {
    var btn = document.getElementById('submitGiftCardContinueBtn');
    btn.click();
    btn.disabled = true;
});

However, it could be a better practice to call the 'on click' method when the document loads, rather than emulate a click on a UI element.

Upvotes: 1

Related Questions