0

Using javascript or jquery what would be the best way to simulate a click on live video inside div id 1.

Im not sure how to handle this when the <a> does not have a id attribute.

 <div id='1'>
       <a href="#" class="edit-settings">Edit Settings</a>
       <a href="javascript:void(0);" class="live_video">Live Video</a>
 </div>

 <div id='2'>
       <a href="#" class="edit-settings">Edit Settings</a>
       <a href="javascript:void(0);" class="live_video">Live Video</a>
 </div>
1
  • 2
    Note that using jQuery to trigger the event will not follow the href property for an <a> (probably what you want), while triggering the DOM click event will Commented Aug 10, 2013 at 23:11

2 Answers 2

2

To trigger the click on the first link.

$("#1 > a:first").trigger('click');

To trigger the click on the second link.

$("#1 > a:last").trigger('click');

You do have CSS classes you can use as well.

$("#1 > a.live_view").trigger('click');

NOTE: a DOM ID can not start with a number.

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

11 Comments

trigger('click') can be changed to click()
so in order to use the name live_video i need to use the a.live_video correct?
@tman that is correct. live_video is a CSS class. You use a dot to reference a class.
perfect Thanks for the note on the DOM ID aswell :) thanks will accept!
any idea how to do this exact line in javascript aswell $("#1 > a.live_view").trigger('click');
|
1

JavaScript:

document.getElementById('2').getElementsByClassName('live_video')[0].click();

Here is a working jsFiddle

I updated it so it answers the following question:

thanks for your edit is there away to specify the a href by the class rather then childnodes – tman

3 Comments

this does not select by the div id which is important.
thanks for your edit is there away to specify the a href by the class rather then childnodes
Although this won't work because .childNodes[0] will return a text node (since there is whitespace in the element), not the <a>. .children[0] could be used in place

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.