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 2 buttons, one has an onclick event that when clicked calls a function that is supposed to add an onclick event handler to the second button.
I was able to do this little example in w3schools.com, here is the code:
!DOCTYPE html html body p Click "Try it" to execute the displayDate() function. /p button id="myBtn2" onclick="addfunc();" Trigger /button button id="myBtn" Try it /button p id="demo" /p script function addfunc(){ document.getElementById("myBtn").onclick = displayDate(); } function displayDate() { document.getElementById("demo").innerHTML = Date(); } /script /body /html
The intended function is that when clicking "Try it" button before "Trigger", nothing happens, and only after clicking "Trigger" the "Try it" button should work. Problem is when I click "Trigger" it actually runs the function that it is supposed to be executed by the click of "Try it" button.
Hope I explained myself, thanks!
javascript html onclick Share Improve this question Follow asked Aug 2, 2017 at 2:17 Juan Solana Juan Solana 147 3 3 silver badges 11 11 bronze badges Add a comment |You should use displayDate instead of displayDate() . Because displayDate() directly calls the function.
!DOCTYPE html html body p Click "Try it" to execute the displayDate() function. /p button id="myBtn2" onclick="addfunc();" Trigger /button button id="myBtn" Try it /button p id="demo" /p script function addfunc(){ document.getElementById("myBtn").onclick = displayDate; } function displayDate() { document.getElementById("demo").innerHTML = Date(); } /script /body /html
If you want to pass variables
!DOCTYPE html html body p Click "Try it" to execute the displayDate() function. /p button id="myBtn2" onclick="addfunc();" Trigger /button button id="myBtn" Try it /button p id="demo" /p script function addfunc(){ document.getElementById("myBtn").addEventListener('click', function(){ displayDate("my params"); }); } function displayDate(params) { document.getElementById("demo").innerHTML = Date() + " -- " + params; } /script /body /html
Share Improve this answer Follow edited Aug 2, 2017 at 2:28 answered Aug 2, 2017 at 2:19 Suresh Atta Suresh Atta 122k 38 38 gold badges 203 203 silver badges 313 313 bronze badges 3 Wow, that was fast! Thanks! What if I want to pass a parameter with the function, say addfunc(x,y), so that they can be used by displayDate? – Juan Solana Commented Aug 2, 2017 at 2:24 @JuanSolana Updated my answer. – Suresh Atta Commented Aug 2, 2017 at 2:28 @SURESHATTA Thanks! That worked! I stayed with .onclick and used the right part of your .addEventListener suggestion! – Juan Solana Commented Aug 2, 2017 at 2:34 Add a comment | 0You can disable the second button (in HTML code) and make a first button an activator of btn1
body main input class="btn1" type="button" value="enable button 2" name="btn1" onclick="ableBtn2()" button Id="btn2" onclick="displayDate()" disabled="true" button 2 /button p id="test" /p script type="text/javascript" function ableBtn2() { document.getElementById("btn2").disabled = false; } /script script type="text/javascript" function displayDate() { document.getElementById("test").innerHTML = Date(); } /script /main /body
Share Improve this answer Follow edited Apr 5, 2020 at 1:31 j3ff 5,989 8 8 gold badges 42 42 silver badges 54 54 bronze badges answered Apr 4, 2020 at 23:32 ecsIsAFK ecsIsAFK 1 1 1 bronze badge Add a comment |To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
defaultSite design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2024.10.3.16276