Click on element by name instead of id in JavaScript

857 views Asked by At

I'm trying to get a few lines of code to click checkbox and button in a website. the elements on the website are shown as following:

<td>
    <label style="display: block; cursor: pointer">
        <input type=radio name="business_9942704" value="2"> Banking Now Inc.
    </label>
</td>

<input type="submit" value="Commit!">

So what it needs to do it click on the name "business_9942704" and then click on the value Commit!

I have tried multiple things but I'm not sure what am I doing wrong, my code looks like this:

document.getElementByName('business_9942704').click(); 
document.getElementByValue('Commit!').click();

What am I doing wrong?

Thanks in advance!

2

There are 2 answers

0
Hien Nguyen On

Correct method is document.getElementsByName('business_9942704')[0].click();

But this method return array of element you need use index for them.

0
Alice On

there is not a function getElementByName

but

getElementsByName

please take a look in the console to see errors, don't program in blind mode