How do i write a code in js that adds images specified ids to a another web div when a button under it is tapped? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do i write a code in js that adds images specified ids to a another web div when a button under it is tapped?

I want when the button under a picture is tapped,the image whose button is tapped is added to another div using DOM manipulation, however i want this to be under one event delegation,that is i dont want to get each and every button and give it a function to add the image tapped,i want to write a code that gets the button tapped and adds the image whose button is tapped. Help please.

3rd Apr 2023, 5:08 PM
Michael Kamau
Michael Kamau - avatar
11 Answers
+ 3
something like this? (ignore the crazy css filters in the imgBtn code. I went overboard playing with them, but that was my base code) The second one (thumbDisplay) is perhaps nearer to what you are describing https://code.sololearn.com/WhoC66mw3gzq/?ref=app https://code.sololearn.com/W9UCgOk26e43/?ref=app
5th Apr 2023, 1:48 AM
Bob_Li
Bob_Li - avatar
+ 2
Still not thoroughly clear. Let me try to break it down ... There are boxes (div) with <img> and <button> within When any one of the <button> under the <img> is clicked; a clone of the respective <img> should be created in another <div> Is this the scenario?
3rd Apr 2023, 6:50 PM
Ipang
+ 2
Michael Kamau No need to copy the code. You should link it instead. Easier to do, and helps a lot more than a wall of pasted code. Edit your question description, tap "+" button, select Code, then choose the code you need help with.
4th Apr 2023, 2:00 AM
Emerson Prado
Emerson Prado - avatar
+ 2
Yeah Bob_Li,thats exactly it,however instead of setting and changing background imgs,i just want to add another img,as a div on its own. With you code i think i can modify it to fit my purpose thank you..
5th Apr 2023, 5:44 AM
Michael Kamau
Michael Kamau - avatar
+ 1
What you refer here by "specified ids"? you mean the 'id' attribute of the <img> or the 'src' attribute? I didn't clearly understood what you meant by "one event delegation". Can you elaborate further on that part? Sharing your code bit link may help anyone to better understand what you mean. So attach your tryout code bit link in the post Description above ...
3rd Apr 2023, 6:25 PM
Ipang
+ 1
However,Ipang the scenario you just described is the one,
4th Apr 2023, 3:29 AM
Michael Kamau
Michael Kamau - avatar
+ 1
Michael Kamau Your idea of not using the src url as background made me realize that the div containers were unnecessary. The divs in imgBtn code were used to hold the emojis The imgs in thumbDisplay code do not need the divs. They can be used on their own. The only essential div is the group flexbox container for the thumbnails. I simplified the code I posted.
5th Apr 2023, 10:40 AM
Bob_Li
Bob_Li - avatar
0
No i meant id attribute of the button Ipang, i need help writing a code that checks for a click on a button that is below an img,after this, it creates a new element on another div and adds the img whose button has been ckicked.
3rd Apr 2023, 6:38 PM
Michael Kamau
Michael Kamau - avatar
0
Im unable to connect the cide bit as i am using my mole phone to ask the question but the code is in my computer,im a bit lazi to copy the code,😢, hopefully you understand my question better Ipang
3rd Apr 2023, 6:40 PM
Michael Kamau
Michael Kamau - avatar
0
I can add the imgs in the div,however i have to check the img whose btn has been clicked and when appending it i have to put its id attribute same as the one in the img and since ids are not the same in the imgs,it becomes a little bit stressful to write each and every code fir the rest of the imgs. The imgs are a lot,is there a way to shorten the code in that when a button is pressed,the target attribute is found and then the img added without need of manually adding it myself
3rd Apr 2023, 6:45 PM
Michael Kamau
Michael Kamau - avatar
0
Ok Prado and Ipang ,i will insert the code
4th Apr 2023, 3:28 AM
Michael Kamau
Michael Kamau - avatar