Your elements are not found because you’re executing the javascript before you’ve added the elements. Try moving the script to the bottom of the body:
<html> <head> </head> <body> <a id="id1">some stuff</a> <a id="id2">stuff</a> <script type="text/javascript"> function click_handler1() { alert("click_handler1"); } function click_handler2() { alert("click_handler2"); } document.getElementById("id1").addEventListener("click", click_handler1, false); document.getElementById("id2").addEventListener("click", click_handler2, false); //window.addEventListener("load", setup, false); </script> </body> </html>
Related Posts:
- How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)
- How can I check for “undefined” in JavaScript?
- How to make history.back() without getting a warning on the browser to reload the page?
- Split string into array
- convert Hsl to rgb and hex
- How to play audio?
- What does jQuery.fn mean?
- “Uncaught TypeError: Cannot read property ‘style’ of null at.. “
- Difference between document.referrer and window.parent.location.href
- JavaScript replace/regex
- how to render a react component using ReactDOM Render
- What does “javascript:void(0)” mean?
- What does this symbol mean in JavaScript?
- What is the difference between state and props in React?
- Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
- OnChange event using React JS for drop down
- how to get the last character of a string?
- Set timeout for ajax (jQuery)
- Escaping Discord subset of markdown
- ESLint Parsing error: Unexpected token
- Angular ForEach in Angular4/Typescript?
- Timers in React Native (this.setTimeout)
- JSLint says “missing radix parameter”
- How to handle ETIMEDOUT error?
- bootstrap-datetimepicker for bootstrap 4
- Proper way to wait for one function to finish before continuing?
- XMLHttpRequest module not defined/found
- How do I create and read a value from cookie?
- Check if a variable is of function type
- Bootstrap 4 Dropdown Menu not working?
- document.getElementsByClassName().innerHTML always returns “undefined”
- Bower: ENOGIT Git is not installed or not in the PATH
- Converting an object to a string
- Phone mask with jQuery and Masked Input Plugin
- JQuery get all elements by class name
- How to change value of object which is inside an array using JavaScript or jQuery?
- How to detect pressing Enter on keyboard using jQuery?
- How to make your Discord Bot always active
- How to write inside a DIV box with javascript
- use Lodash to sort array of object by value
- Chrome/jQuery Uncaught RangeError: Maximum call stack size exceeded
- How to make an HTML back link?
- How to fix getImageData() error The canvas has been tainted by cross-origin data?
- How to make fadeOut effect with pure JavaScript
- How to get unique values in an array
- How to fix Error: listen EADDRINUSE while using NodeJS?
- What is ‘xmlhttp.setRequestHeader();’ and in which situations is it used?
- Write / add data in JSON file using Node.js
- Set scroll position
- .filter is not a function
- Function overloading in Javascript – Best practices
- Wait until flag=true
- momentJS date string add 5 days
- When is a CDATA section necessary within a script tag?
- How to get scrollbar position with Javascript?
- where is create-react-app webpack config and files?
- What does wp-embed.min.js do in WordPress 4.4?
- Gutenberg extend blocks add new class name
- How do I add custom attributes to javascript tags in WordPress?
- How to wrap the content of the main tinyMCE editor with extra tags
- Rest API authentication issue when called from fetch request in bundle.js
- Javascript not working?
- Add Item to Custom TinyMCE Menu
- Listbox return wrong value in WordPress 3.9
- Logout user and delete cookies from the client side?
- wp_enqueue_script & constants?
- How do developers add “Smooth Transitions” to their sites?
- Resolving Javascript errors likely related to Gantry framework
- How can I get my Calculate Contact Form 7 field to update with jQuery? [closed]
- I am trying to add current logged in user to my zoho chat
- Where to upload JavaScript file in WordPress
- Calling a WordPress Custom JavaScript file
- Conditional save return on Gutenberg Block
- Can I alter the block editor’s paste text behavior?
- Custom Block – save.js function not saving attributes
- Headless wp with react. How to handle routes?
- Issues trying to get an element by id
- How do I know if I should enqueue JS code or just include it in ONE PHP function?
- How do I use this javascript correct with wordpress?
- How can I integrate yepnope.js with WordPress (and plugins)?
- How to set javascript options for select2 in PHP array of plugin
- Multiple jQuery inputs or binds in Customizer Control Javascripts
- How to display different content on the same page using different URLS
- Change background video when scrolling reaches a certain ID
- Immediate (e.g. without DOMContentLoaded) JS not working
- Dynamically changing logo on scroll function [closed]
- Script not working in post content
- How to identify which javascript is being executed
- Remove WordPress default registration client side validation?
- How could I add load more posts to my theme? [closed]
- How to keep close a sub-menu under homepage? [closed]
- How to implement custom URL handler to access JS modal content? [closed]
- Javascript button to call custom fields data
- Javascript 2 elements dependent onclick .toggle [closed]
- Table block variation with header section activated
- How To Include JS Script with WP 2023 Theme
- Update js file on wordpress page after making changes
- Gutenberg core/file add style support in js/ json
- uploading image to wordpress site with javascript fetch function failed