html - How to trigger click event using JavaScript on querySelectorAll -


in jquery have following code

$("li#credit").trigger('click'); 

this code triggers click event on li has id credit.

now using javascript

document.queryselectorall("li#credit").click(); 

i getting error:

click() not function

the page working have multiple elements id "credit", need trigger click event on <li> has id credit.

queryselectorall returns nodelist object (source) , not dom element. nodelist object contains dom elements.

your code needs change first element , call click():

document.queryselectorall("li#credit")[0].click(); 

if want trigger .click() event elements can for loop

for(var = 0; i<elems.length; i++) {     elems[i].click(); } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -