WhizBoy
WhizBoy

Reputation: 225

JavaScript get recognize button to click

I am making a Chrome extension and I need this button to be clicked so how can I get the click to be recognized?

<button class="btn btn-lg btn-danger btn-bet" onclick="system.play.bet.red();">RED</button>

I tried var test= document.getElementsByClassName("btn btn-lg btn-black btn-bet"); test.click();

but it fails at the beginning.

Upvotes: 1

Views: 140

Answers (1)

Johannes Jander
Johannes Jander

Reputation: 5020

Document.getElementsByClassName() returns an array-like object of all child elements which have all of the given class names. Unfortunately, it's not a true JS array. To work with it, you need to "Arrayify" it:

var testElements = document.getElementsByClassName('test');
var testDivs = Array.prototype.filter.call(testElements, function(testElement){
    return testElement.nodeName === 'DIV';
});

In your case, this should work:

var test= document.getElementsByClassName("btn btn-lg btn-black btn-bet")[0];
test.click();

Upvotes: 1

Related Questions