Trigger click event of an anchor tag with javascript/jquery



getElementsByClassName doesn"t return an element but a NodeList which may contain more than one element.

Bạn đang xem: Trigger click event of an anchor tag with javascript/jquery

You may vị this :

document.getElementsByClassName("some-iclass")<0>.click();or if you want to click all elements :

var danh sách = document.getElementsByClassName("some-iclass");for (var i=0; iBut as you use jQuery, it would be simpler lớn do

$(".some-iclass").click();but only when the click sự kiện handler was added with jQuery (in other cases, lượt thích for example in case of an href attribute, use the standard dom functions).

chia sẻ
Improve this answer
edited Oct 17 "13 at 12:20
community wiki
2 revsdystroy
showroom a phản hồi |
$(document).ready(function() $(".some-iclass").trigger("click"); );
chia sẻ
Improve this answer
answered Oct 17 "13 at 11:57

7,69899 gold badges4343 silver badges7171 bronze badges
showroom a bình luận |
Simple with jquery $(".some-iclass").click();

if you have a lot of elements with this class - point to lớn the wanted element:i.e. $($(".some-iclass")<0>).click();

mô tả
Improve this answer
edited Oct 17 "13 at 12:04

4,40933 gold badges2525 silver badges4747 bronze badges
answered Oct 17 "13 at 11:58

griffon vulturegriffon vulture
6,00666 gold badges3434 silver badges5656 bronze badges
địa chỉ a bình luận |
for auto-clicking a button or a link


this works...:)

nội dung
Improve this answer
edited Jan 17 "14 at 13:32
answered Jan 17 "14 at 13:25
1122 bronze badges
địa chỉ cửa hàng a phản hồi |
if you want khổng lồ autoclick a link and you are using jQuery, you could use

$(".yourClass").click();if you need this to lớn be one link in a collection of multiple links, you could bởi this:

$($(".yourClass")<0>).click();Where 0 is the index of the element in the jQuery object.

document.getElementsByClassName("yourClass"); does not work in older browsers so it"s best khổng lồ use jQuery here for cross-browser compatibility.

Xem thêm: Trồng Hoa Hồng Tỉ Muội Trong Chậu !, Hoa HồNg Tỉ MuộI

Improve this answer
answered Oct 17 "13 at 11:59
4,40933 gold badges2525 silver badges4747 bronze badges
showroom a comment |
For me, I managed to make it work that way. I deployed the automatic click in 5000 milliseconds và then closed the loop after 1000 milliseconds. Then there was only 1 automatic click.

giới thiệu
Improve this answer
answered Feb 7 "17 at 13:01
Naiguel DeveloperNaiguel Developer
30333 silver badges33 bronze badges
showroom a comment |

Your Answer

Thanks for contributing an answer khổng lồ Stack Overflow!

Please be sure to answer the question. Provide details & share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using thư điện tử and Password

Post as a guest

e-mail Required, but never shown

Post as a guest

thư điện tử

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy & cookie policy

Not the answer you're looking for? Browse other questions tagged javascript jquery or ask your own question.

The Overflow Blog
Featured on Meta
Visit chat
How can I change an element's class with JavaScript?
Abort Ajax requests using jQuery
How vày I chop/slice/trim off last character in string using Javascript?
Get selected value in dropdown list using JavaScript
How can I select an element by name with jQuery?
Scroll lớn the top of the page using JavaScript?
Convert size data to JavaScript object with jQuery
Get selected text from a drop-down list (select box) using jQuery
Get class name using jQuery
pretty-print JSON using JavaScript
Hot Network Questions more hot questions

Question feed
Subscribe lớn RSS
Question feed to lớn subscribe to lớn this RSS feed, copy & paste this URL into your RSS reader.

Stack Overflow
Stack Exchange Network
site thiết kế / biểu tượng logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.2.3.41361

Stack Overflow works best with JavaScript enabled

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device & disclose information in accordance with our Cookie Policy.