ekspresyon
ekspresyon

Reputation: 25

click() method not working Console returning error JavaScript

I'm Trying to create a set of actions with the click of a "Button B"
which will click on "Button A" as the last action of the function triggered by B.
I have used the same line of code elsewhere. I can't figure out why I keep getting errors for the last action call.

Please help me understand I have researched quite a bit can't find an answer.

function doActionsA(){
  document.getElementById('view').innerHTML = "You clicked?";
}

function doActionsB(){
//Other actions befor the click action.

//I used this befor and it worked in other instances
document.getElementsByClassName('active').click();
}
<div id="view"></div>
<button class="active" onclick="doActionsA();">Button A</button>
<button class="trigger" onclick="doActionsB();"> Button B</button>

Upvotes: 0

Views: 165

Answers (1)

sridhar..
sridhar..

Reputation: 2133

document.getElementsByClassName returns an array of elements. So you need to acces individual element and then perform click operation

function doActionsA(){
  document.getElementById('view').innerHTML = "You clicked?";
}

function doActionsB(){
//Other actions befor the click action.

//I used this befor and it worked in other instances
document.getElementsByClassName('active')[0].click();
}
<div id="view"></div>
<button class="active" onclick="doActionsA();">Button A</button>
<button class="trigger" onclick="doActionsB();"> Button B</button>

Upvotes: 2

Related Questions