-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.

Community
  • 1
  • 1
user2683109
  • 3
  • 1
  • 5
  • 1
    if you're getting null, then the element doesn't exist when you're executing that code. Please check that at first. – Samuli Hakoniemi Oct 01 '14 at 11:42
  • what is data-cancelable="true" doing? – Keith Oct 01 '14 at 11:43
  • What does this have to do with JSON? – Horst Gutmann Oct 01 '14 at 11:43
  • Chances are you are running the code before the element in your page without using onload. Please verify – Spokey Oct 01 '14 at 11:44
  • Click action is working. Check this fiddle http://jsfiddle.net/oak7ncsv/ – Aravindhan Oct 01 '14 at 11:44
  • I don't think starting an Id with a number is valid - try using a letter to start the Id instead http://stackoverflow.com/a/79022/1370442 – Luke Baughan Oct 01 '14 at 11:47
  • 1
    @bUKaneer Only for HTML4 – CodingIntrigue Oct 01 '14 at 11:52
  • Did you try looking here http://stackoverflow.com/questions/2381572/how-can-i-trigger-a-javascript-event-click ? – Beri Oct 01 '14 at 12:00
  • Most likely that anchor tag's id is created dynamically. So when you try to target it by ID, it will return null since that ID is changed each time. Did you inspect your page that the anchor tag actually have the same ID?? – Huangism Oct 01 '14 at 14:53
  • I am trying to automate clicking on this 'a' tag. I am trying this from console which we get by inspecting element in chrome. id is not dynamically created, I verified that couple of times. This website hasn't been created by me, and it works pretty well when I am manually clicking on that link. I did 'document.getElementById('9ddfe6c10a0a0b7e0062ccf1e8afc011').href' but it again returned null. – user2683109 Oct 01 '14 at 17:08
  • @zvona- I am unsure of this as I haven't created this site. – user2683109 Oct 01 '14 at 17:19
  • @HorstGutmann- Sorry if this doesn't relate to JSON. My lack of scripting XP to blame :( ... I was trying to use either to accomplish clicking... – user2683109 Oct 01 '14 at 17:20
  • @Huangism- I checked this many times, it is static... – user2683109 Oct 01 '14 at 17:21
  • @Aravind- It's working indeed. I did some changes and that too worked. So why is console on google chrome not getting this, confused! – user2683109 Oct 01 '14 at 17:34

1 Answers1

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...

Willem
  • 61
  • 2
  • 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 :) – user2683109 Oct 01 '14 at 17:15
  • Try: $('#9ddfe6c10a0a0b7e0062ccf1e8afc011')[0].click(); That should also work. – Willem Oct 01 '14 at 18:38
  • 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' – user2683109 Oct 01 '14 at 19:54
  • What happens when you try $('.menu'); ? – Willem Oct 01 '14 at 20:05