-1

I have gone through Simulate a click on 'a' element using javascript/jquery but it didn't help.

The code I am running against is

<a target="gsft_main" id="9ddfe6c10a0a0b7e0062ccf1e8afc011" data-cancelable="true" class="menu" href="discovery_status_list.do?sysparm_userpref_module=9ddfe6c10a0a0b7e0062ccf1e8afc011">Status</a>

I have executed below code

document.getElementById('9ddfe6c10a0a0b7e0062ccf1e8afc011').click()

but it doesn't work. I also tried

$('#9ddfe6c10a0a0b7e0062ccf1e8afc011').trigger('click')

with no success. If I run

document.getElementById('9ddfe6c10a0a0b7e0062ccf1e8afc011')

it returns null.

14
  • 1
    if you're getting null, then the element doesn't exist when you're executing that code. Please check that at first. Commented Oct 1, 2014 at 11:42
  • what is data-cancelable="true" doing? Commented Oct 1, 2014 at 11:43
  • What does this have to do with JSON? Commented Oct 1, 2014 at 11:43
  • Chances are you are running the code before the element in your page without using onload. Please verify Commented Oct 1, 2014 at 11:44
  • 1
    @bUKaneer Only for HTML4 Commented Oct 1, 2014 at 11:52

1 Answer 1

0

To trigger an 'a' element directly doesn't work in this case. If you wrap your text in a span and trigger a click event on the span it should work.

<a id="test" href="http://stackoverflow.com"><span>Status</span></a>

and with jquery:

$('#test span').trigger('click');

if you get a null on document.getElementById('9ddfe6c10a0a0b7e0062ccf1e8afc011') you probably made a typo...

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

4 Comments

Can't span it as I have not created this website. I am trying to automate click on that link after every, say 5 seconds or so. I am trying this from console provided by chrome. I also tried 'document.getElementById('9ddfe6c10a0a0b7e0062ccf1e8afc011').href' but this too returns null. But it works well when I click on that link manually. Since I have less XP in scripting, I am unsure of many things. But since I know coding, I am able to get my head around a few :)
Try: $('#9ddfe6c10a0a0b7e0062ccf1e8afc011')[0].click(); That should also work.
I get 'TypeError: Cannot read property '0' of null' If I run the command normally, it is returning 'null' all the time. I think may be this is the problem. When I simply execute '$('#9ddfe6c10a0a0b7e0062ccf1e8afc011')', it gives 'null'
What happens when you try $('.menu'); ?

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.