2

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();
});
8
  • 2
    what code is being executed in the click event? Commented Nov 2, 2011 at 20:59
  • @AbeMiessler There is no code in the event, it is as it is... Perhaps the problem? Like i said it successfully clicks the button after the page loads, but it goes into a loop of clicking, i need it to just click it and either wait or just do it once. Commented Nov 2, 2011 at 21:03
  • 3
    If that is your code, it looks fine. There is a chance that when you submit your form, it refreshes the page, which causes the .ready state to trigger again. That will give you the infinite loop you are experiencing. Commented Nov 2, 2011 at 21:03
  • 2
    So... What happens when the button is clicked? Does the page reload? Is a form submitted? Is an event handler fired? Commented Nov 2, 2011 at 21:04
  • 1
    Why do you want the button to be clicked when the page loads? Commented Nov 2, 2011 at 21:07

2 Answers 2

2

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

IS there a way without disabling it, we have similar situation but the button should not be disabled rather only once during the first loading of the page @Jakub Arnold
1

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.

2 Comments

I'm not sure why but that code doesnt work, i mean it looks like it would, but it doesnt click the button. I tested it with the btn.disabled line and without, which is weird because without it its pretty much the same code just using it as a variable. Any ideas?
Thanks, that didnt do the trick. I think its the scripting on the page im trying this on. I think its being reset from some AJAX refresh that validates or gives an error for the code. I've selected your answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.