Javascript Doubt | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Javascript Doubt

I have doubt in js that is, how can I display an image in the website when a button is clicked ( that will not display automatically- I want it to display only when button is clicked) by accessing the id ? Everytime I try I do some mistakes. Please help me.

21st Feb 2022, 3:59 AM
Aurora Borealis
Aurora Borealis - avatar
11 Answers
21st Feb 2022, 3:28 PM
Chetali Shah
Chetali Shah - avatar
+ 1
Use the onClick() function of javascript and the display property of css. https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp
21st Feb 2022, 4:27 AM
Chetali Shah
Chetali Shah - avatar
+ 1
Inside your function show(), write this: let z = document.getElementById("yourimgid"); z.style.display="block";
21st Feb 2022, 3:08 PM
Chetali Shah
Chetali Shah - avatar
0
Thanks Chetali Shah . But how can I add destination of the div to the button in js?
21st Feb 2022, 5:32 AM
Aurora Borealis
Aurora Borealis - avatar
0
Gursimran Kaur You can fetch the ID of your div element inside your js script by using getElementById(). Look at the link that I shared earlier, it has an example which will give you clarity.
21st Feb 2022, 1:23 PM
Chetali Shah
Chetali Shah - avatar
0
Chetali Shah okay, I got it. But I want that my button should display image not change color.
21st Feb 2022, 2:41 PM
Aurora Borealis
Aurora Borealis - avatar
0
Then you fetch your img element by the getElementById() method.
21st Feb 2022, 2:55 PM
Chetali Shah
Chetali Shah - avatar
0
You have to replace the "yourimgid" with the id of your image and the onclick event will go inside the button and not in your img tag.
21st Feb 2022, 3:13 PM
Chetali Shah
Chetali Shah - avatar
21st Feb 2022, 3:27 PM
Aurora Borealis
Aurora Borealis - avatar
0
The onclick() event will go inside your button tag and not in your img tag.
21st Feb 2022, 3:28 PM
Chetali Shah
Chetali Shah - avatar
0
Thanks Chetali Shah, you helped me a lot!😊
21st Feb 2022, 3:41 PM
Aurora Borealis
Aurora Borealis - avatar