1

I want to do two click event in Javascript, 1 after another. First click code is working fine but the second click is not happening. For the second click, a window pops up after 0.5 seconds.

I am using the below code, I am new to JS and don't know what is the issue

var x = document.getElementsByClassName('artdeco-button--secondary');
for (var i = 0; i < x.length; i++ ){
    x[i].addEventListener('click',function(){

        document.getElementsByClassName('ml1').click();
    });

}
3
  • Can you explain where the clicks should be occurring and also provide the html so that a user can debug if needed? I think with a little more editing you will have a very clear question that will get a great answer! Commented May 4, 2019 at 14:39
  • 1
    check whether document.getElementsByClassName('ml1') is returning an array or single element. maybe you still have to run a loop to click all the elements with class name ml1 Commented May 4, 2019 at 14:53
  • It is on linkedin, where I need to click on "connect" then a pop appears with 2 more buttons "add note" or "send invite". First the code should click on "connect" then "send invite" with for loop Commented May 4, 2019 at 16:43

2 Answers 2

2

You should use dblclick as event in addEventListener instead of click

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

Comments

0
// you can do the same as follows
var buttons = document.querySelectorAll('.artdeco-button--secondary');

for (const button of buttons) {
  button.addEventListener('click', function() {
    // but here, you are trying to apply
    // an event to a list of Elements with getElementsByClassName
    // if you want to execute a click event to an specific element
    // you can do it as follows
    document.querySelector('#elementId').click();
    // or using the index of the element in an array
    const target = document.querySelectorAll('.target')[0];
    target.click();
    // or iterating the list to apply it to each element
    const targets = document.querySelectorAll('.target');
    for (const target of targets) {
      target.click();
    }
  });
}

Comments

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.