Skip to content Skip to sidebar Skip to footer

Click A Button On A Web Site Automatically With Javascript

I modify a code like that, for click a checkbox. Such as, Is there a any problem about button name or id. I could see just name and class. Is this a problem for work?

Solution 1:

Problem 1: getElementById should be getElementByName

Based on your screenshot, the input item you are trying to reference is:

<input name="chkA5" class="formCheckBox" type="checkbox" value="ON"></input>

and you are trying to getElementById()

document.getElementById("chkA5").checked = true

However, there is no id declared, so you will have to get the item by the name, using getElementByName():

document.getElementsByName("chkA5")[0].checked = true;

Problem 2: Your javascript has errors

This line will cause your script block fail:

var chkA5 = "button" class=formCheckBox type=checkbox value=ON name=chkA5

If you require a complete code sample, here is an example:

<html>
<head>
</head>
<body>
    <input name="chkA5" class="formCheckBox" type="checkbox" value="ON"></input>
</body>
<script>
    (function() {
        document.getElementsByName("chkA5")[0].checked = true;
    })();
</script>
</html>

Note: Make sure that the script block is at the end of your html, like in your example code provided.


Post a Comment for "Click A Button On A Web Site Automatically With Javascript"