1

I have a button(hyperlink) with its equivalent HTML code as:

<span title="Enroll in Classes" class="SSSBUTTON_CONFIRMLINK">
<a class="SSSBUTTON_CONFIRMLINK" href="javascript:submitAction_win0(document.win0,'DERIVED_REGFRM1_LINK_ADD_ENRL$118$');" tabindex="202" ptlinktgt="pt_peoplecode" id="DERIVED_REGFRM1_LINK_ADD_ENRL$118$" name="DERIVED_REGFRM1_LINK_ADD_ENRL$118$">Proceed to Step 2 of 3</a>
</span>

I want to click the button 10 seconds after the page is loaded and I use the following code:

// ==UserScript==
// @name        Add to cart
// @namespace   Class
// @description Script to add to cart
// @include     http://go.oasis.com/add/?STRM=2141
// @version     1
// @grant       none
// ==/UserScript==

setInterval (clickOnProceedButton, 10 * 1000)

function clickOnProceedButton () {
    var targSubmit  = $("#DERIVED_REGFRM1_LINK_ADD_ENRL$118$");
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    targSubmit[0].dispatchEvent (clickEvent);
}

The above script is not working and does not click the button. Can someone point out what is wrong here, please?

EDIT: I get the following error in the Console

ERROR: Execution of script 'Add to cart' failed! Object [object global] has no method 'clickOnProceedButton'
TypeError: Object [object global] has no method 'clickOnProceedButton'
    at Object.eval (unknown source)), 25:14)
    at Object.eval (unknown source)), 27:4)
    at ag (unknown source), 190:4)
    at K (unknown source), 190:46)
    at o (unknown source), 456:2)
    at U (unknown source), 460:85)
    at R (unknown source), 229:40)

Thanks in advance!

Rajath
  • 2,178
  • 6
  • 32
  • 38

2 Answers2

1

Perfectly working in Google Chrome but not working in IE8

JSFiddle

HTML:

<a  href="https://google.com"  id="DERIVED_REGFRM1_LINK_ADD_ENRL$118$" >Proceed to Step 2 of 3</a>

JS:

function clickOnProceedButton () {
var targSubmit  =  document.getElementById("DERIVED_REGFRM1_LINK_ADD_ENRL$118$");
var clickEvent  = document.createEvent('MouseEvents');
clickEvent.initEvent ('click', true, true);
targSubmit.dispatchEvent(clickEvent);

}

might help you.

SK.
  • 4,174
  • 4
  • 30
  • 48
1
setInterval(clickOnProceedButton, 10 * 1000);
function clickOnProceedButton() {
    var link = document.getElementById("DERIVED_REGFRM1_LINK_ADD_ENRL$118$");
    if(link) {
        link.click();
    }
}

Demo

zanetu
  • 3,740
  • 1
  • 21
  • 17
  • It seems document.getElementById() is not retrieving anything. So clicking action is not happening. Your demo works fine. – Rajath Jan 16 '14 at 01:49