A puzzlement about TagName

Joined
Jan 6, 2022
Messages
24
Reaction score
0
Shouldn't this work? The alert should fire when a box is clicked but it doesn't

Code:
<script>
function go(){
    foo=getElementsByTagName('input')
    alert('the function worked')
}
</script>

    <!---HTML--->
check this-> <input type='checkbox' onchange="go()">
or this-> <input type='checkbox' onchange="go()">
 
Joined
Jul 4, 2023
Messages
550
Reaction score
72
Check this out:
HTML:
<script>
  function go(element) {
    alert(
      'You clicked on the checkbox:\n' +
      `name: ${element.name}\n` +
      `value: ${element.value}`
    );
  }
</script>

<label>check this-> <input type="checkbox" name="checkbox_1" onchange="go(this)" value="lorem_1"></label>
<label>or this->    <input type="checkbox" name="checkbox_2" onchange="go(this)" value="lorem_2"></label>

or
HTML:
<script>
  function go(e) {
    alert(
      'You clicked on the checkbox:\n' +
      `name: ${e.target.name}\n` +
      `value: ${e.target.value}`
    );
  }
</script>

<label>check this-> <input type="checkbox" name="checkbox_1" onchange="go(event)" value="lorem_1"></label>
<label>or this->    <input type="checkbox" name="checkbox_2" onchange="go(event)" value="lorem_2"></label>

or
HTML:
<script>
  function go(e) {
    if (e.target.matches('input[type="checkbox"]')) {
      alert(
        'You clicked on the checkbox:\n' +
        `name: ${e.target.name}\n` +
        `value: ${e.target.value}\n` +
        `is checked: ${e.target.checked.toString()}`
      );
    }
  }
</script>

<div onclick="go(event)">
  <label>check this &#8680; <input type="checkbox" name="checkbox_1" value="lorem_1"></label>
  <label>or this &#8680;    <input type="checkbox" name="checkbox_2" value="lorem_2"></label>
</div>


[ HTML symbol codes and entities ]
 
Last edited:
Joined
Aug 22, 2023
Messages
60
Reaction score
14
You have made a few syntactical errors.
-Remember to close particular sections of the script with semicolons (script was necessary in this case).
-You are using the keyword "onchange". This keyword executes a designated function when some attribute is changed. The "onclick" keyword or the "addEventListener" keyword would be more appropriate for firing an alert when a box is clicked.
HTML:
<!DOCTYPE html>
<html>
    <head>
        <title>Alert Execution Using "onclick" Keyword</title>
    </head>
    <body>
        <input type="button" value="Click me!" onclick="alert('The function worked!');"/>
    </body>
</html>
P.S. For this code snippet, document.getElementsByTagName was not needed, but if you would like some primer on it it is a variation of the generic querySelector() method. The attribute this method is getting is "TagName".

Hope this was helpful!
 
Last edited:
Joined
Jul 4, 2023
Messages
550
Reaction score
72
querySelector() method. The attribute this method is getting is "TagName".
No, "TagName" is not a parameter for querySelector() in the strict sense of the word.


element.querySelector(CSS selectors)

ParameterTypeDescription
CSS selectorsStringRequired. Specifies one or more CSS selectors to match the element. These are used to select HTML elements based on their id, classes, types, attributes, values of attributes, etc.

For multiple selectors, separate each selector with a comma. The returned element depends on which element that is first found in the document (See "More Examples").

Tip: For a list of all CSS Selectors, look at our CSS Selectors Reference.
 
Joined
Jan 6, 2022
Messages
24
Reaction score
0
Ah, I just found the problem, my original posted code had
Code:
foo=getElementsByTagName('input')

which should have been...
Code:
foo=document.getElementsByTagName('input')
 
Joined
Jan 6, 2022
Messages
24
Reaction score
0
Ah, I just found the problem,
Here's the faulty line
Code:
foo=getElementsByTagName('input')

which should be

Code:
foo=document.getElementsByTagName('input')


Thanks all for the replies. I always learn from them
(and I will use "onclick" instead of "onchange")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,749
Members
47,302
Latest member
MitziWragg

Latest Threads

Top