getElementsByTagName returns an array, not a single element. You need to loop through your results and attach a function to each one individually, something like this:
var buttons = document.getElementsByTagName('button');
for( var x=0; x < buttons.length; x++ ) {
buttons[x].onclick = function(){
alert('entered');
document.getElementById("demo").innerHTML="test";
};
}
Related Contents:
- Do DOM tree elements with IDs become global properties?
- Find object by id in an array of JavaScript objects
- Is there a RegExp.escape function in JavaScript?
- How do I access previous promise results in a .then() chain?
- XML parsing of a variable string in JavaScript
- How to loop through a plain JavaScript object with the objects as members
- How to calculate date difference in JavaScript?
- Communication between browser tabs/windows using JavaScript [duplicate]
- Check whether user has a Chrome extension installed
- How can I parse a string with a comma thousand separator to a number?
- Mongoose and multiple database in single node.js project
- Why shouldn’t JSX props use arrow functions or bind?
- How does AJAX work?
- How to change href of tag on button click through javascript
- How to get all selected values of a multiple select box?
- Why does Google prepend while(1); to their JSON responses?
- Merge multiple objects inside the same array into one object [duplicate]
- Accessing Variables from Greasemonkey to Page & vice versa
- How to replace remapColums with remapColumnsByName in free jqgrid
- Mongoose auto increment
- Determine which element the mouse pointer is on top of in JavaScript
- How can I check if the array of objects have duplicate property values?
- Pass vars to JavaScript via the SRC attribute
- How to pass data to url from jqgrid row if hyperlink is clicked
- Discord.js v12 code breaks when upgrading to v13
- Check if third-party cookies are enabled
- this operator in javascript
- Socket.io 1.x: use WebSockets only?
- How to create a custom scrollbar on a div (Facebook style)
- react native use variable for image file
- How to get text of an input text box during onKeyPress?
- Uploading ‘canvas’ image data to the server
- keyCode values for numeric keypad?
- Change default timeout for mocha
- jQuery Selector + SVG Incompatible?
- When using jQuery on(), why use (document) vs. the element itself?
- Why does “alert(3>2>1)” alert “false” [duplicate]
- Express js form data
- Function and variable with the same name
- How do you detect when CSS animations start and end with JavaScript?
- Inserting text at cursor in a textarea, with Javascript
- Correct way to document open-ended argument functions in JSDoc
- UI-router interfers with $httpbackend unit test, angular js
- Setting value of Observable not updating in Knockout
- Objects don’t inherit prototyped functions
- Encrypt string in PHP and decrypt in Node.js
- What is the benefit of prepending async to a function that returns a promise?
- How do I hide the VueJS syntax while the page is loading?
- Getting timestamp from mongodb id
- HTML Tags in Javascript Alert() method