button button click
Communities for your favorite technologies. Explore all Collectives
TeamsNow available on Stack Overflow for Teams! AI features where you work: search, IDE, and chat.
Learn more Explore Teams TeamsAsk questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about LabsI have a button that I would like to assign a click event to, so that when I click on the button it will run the function.
button id="buttonid" /button
When I run the script below it will trigger the function, but the click event will not be added to the button. So I cannot use the button to trigger the function.
function myFunction() { alert("Hello, world!"); } var element = document.getElementById("buttonid"); element.onclick = myFunction;I believe my goal would look like this:
button id="buttonid" onClick="myFunction()"
javascript function buttonclick Share Improve this question Follow asked Dec 17, 2018 at 14:36 Joe Berg Joe Berg 381 6 6 silver badges 19 19 bronze badges Add a comment |Your initial code should work, provided that it is run after the DOM is ready so that the document.getElementById("buttonid"); actually finds the element.
Check your console for errors that might indicate that element is undefined .
To make sure move the script tag that includes your code at the bottom ( right before closing the body tag )
After the above a better approach is to not set the onclick attribute but instead use the addEventListener method.
body button id="buttonid" clickme /button script function myFunction() { alert("Hello, world!"); } var element = document.getElementById("buttonid"); element.addEventListener('click', myFunction); /script /body
Share Improve this answer Follow answered Dec 17, 2018 at 14:39 Gabriele Petrioli Gabriele Petrioli 195k 34 34 gold badges 270 270 silver badges 325 325 bronze badges 4 Hi Gabriele, thank you for your answer. I tried both your and my example above, and both actually works if I try to run them from console in Chrome here on stackoverflow. But at least my example above didn t work in Safari at work, and the DOM is ready, as I waited for the page to load before injecting the script from console, and the button id is also correct. In my case above, would it matter if 1. it s Safari browser, 2. injected by a script/extension or 3. the buttons are replaced spans: stackoverflow.com/questions/53803268 you can read here where I replaced the spans with buttons – Joe Berg Commented Dec 17, 2018 at 16:05 My end goal is to have the replaced spans (which are now buttons) clickable and have the same function assigned to all of them. The function will behave differently dependent on what is in the inner text of each button. – Joe Berg Commented Dec 17, 2018 at 16:10 @JoeBerg It will not work with the code in that answer. You need to alter that script to maintain the id of the span to the button and run the code that adds the event handler after the code that replaces the spans. – Gabriele Petrioli Commented Dec 17, 2018 at 16:23 1 Thanks for pointing that out Gabriele, that should have been the issue. I used the same method as in the other question to assign a new id to the buttons, and it worked now. :) – Joe Berg Commented Dec 18, 2018 at 6:23 Add a comment | 0Try this:
function myFunction(e) { alert("Hello, world!"); console.log(e); } button id="buttonid" onclick="myFunction(event)" Click me /button If you wanna add event handler from js code then that code should be included blow your html with button.
Share Improve this answer Follow edited Dec 17, 2018 at 14:44 answered Dec 17, 2018 at 14:39 Kamil Kiełczewski Kamil Kiełczewski 91.1k 32 32 gold badges 392 392 silver badges 362 362 bronze badges Add a comment |To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-jsSite design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2024.10.3.16276