-1

I modify a code like that, for click a checkbox.

Such as, My problem is

Is there a any problem about button name or id. I could see just name and class. Is this a problem for work?

<html>
<head>
</head>
<body>
<a href="My_Link" class="wp_textBox"><button>Giris</button></a>
</body>
        <script type="text/javascript">
        var chkA5 = "button" class=formCheckBox type=checkbox value=ON name=chkA5
         window.onload = function() {
          document.getElementById("chkA5").checked = true
        });
        }
        </script>

</html>

I copied all chechbox button properties on web site (F12 + Slect Element + click to check box) and pasted in my script. But I really confused, when I write a code in script, describing works for new things which I add or create. On this web site which I want to click a chechbox on has already buttons and text/check box. How can I create a connect with each other my scrips and web site.

In brief; I couldn't connect my scripts to web site's button and because of that I couldn't do any operation. Am I right?

How can I solve this problem? On picture which I shared, there are some code marked in a red square. This code works for desciribe some element in my scribs?

When we use document.get.ElementById().checked =true, on web site's element properties's has not a id? It has name and class.

11
  • 1
    Possible duplicate of HTML How to auto select a check box? Commented Feb 21, 2017 at 11:40
  • The code looks incomplete. Commented Feb 21, 2017 at 11:48
  • @JonyD I saw this tag but ,It is in the same html, I want to click a checkbox on a web site that Already works. This kind of method work for your own check box which created on your html. I don't create a check box because of that how can I interfere on input line. I need to interfere web site's checkbox Commented Feb 21, 2017 at 11:59
  • @Svek I saw that after I shared it was id and I changed for try after that I forget. But still don't work even I changed to as "id" again. Commented Feb 21, 2017 at 12:00
  • what is var chkA5 = "button" class=formCheckBox type=checkbox value=ON name=chkA5 doing in the <script> block? You might want to review your code... Commented Feb 21, 2017 at 12:04

1 Answer 1

0

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.

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

2 Comments

when I tried ; <input name="chkA5" class="formCheckBox" type="checkbox" value="ON"></input> that's mean is creating new checkbox.. I need to interfere a checkbox on web site. Already open and using web site.
@MustafaCoskun I think you are confused with my answer. Try replacing the last code block of my answer with yours.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.