how to change elements with onclick | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how to change elements with onclick

how to use js to replace elements by others when clicked on a button , and how to replace elements with inputted ones

14th Sep 2019, 9:46 PM
Ouardi Yacine
Ouardi Yacine - avatar
5 Answers
+ 3
document.querySelector("Element").addEventListener('click', function(){ What to do when the element is clicked }); If the element is class don't forget the "." Before the class name If it's I'd don't forget the "#" If it's a tag just put it's name But Note the element should be in quote
15th Sep 2019, 2:55 AM
Qudusayo
Qudusayo - avatar
+ 1
That's should help u started maybe, but be more specific pls
14th Sep 2019, 11:08 PM
Minerals2016
Minerals2016 - avatar
+ 1
<style> .paintred {color="red";} </style> <div id="mydiv"> my element </div> <button onclick="myfunction()"> click me </button> <script> function myfunction() { var el = getElementById('mydiv'); el.setAttribute('class', 'paintred'); </script>
14th Sep 2019, 11:35 PM
wenz
wenz - avatar
0
What do you mean by "replace elements by others"? what is this "others"? Also what do you mean by "replace elements with inputted ones"? what is inputted? how this input is read? Please rephrase the Description and if possible add examples what to replace, which replaces what etc. Hopefully with clearer description more people will come to respond. Good luck! 👍
14th Sep 2019, 10:51 PM
Ipang
0
<input type="buttob" onclick = "test()"> function test() { console.log("it works"); }
14th Sep 2019, 11:08 PM
Minerals2016
Minerals2016 - avatar