How to choice particular div | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to choice particular div

Hello Sololearn,it is really hard to explain, i have a lot of divs with simillar class name and diffrent text on it, then button which send this text to another page using innerHTML, but I cant choice particular div when i click button,if I dont misunderstood it could be done with "child".(I dont remember sorry)) P.S:class must be simillar and without Php

18th Mar 2019, 5:52 AM
Fury
7 Answers
+ 5
Fury, if the div is placed before the button then you can try this script. Map the function to the onclick attribute of the buttons, this will get the button's previous sibling and show it's text in alert window. Elaborate more if I misunderstood your intention. function showText() { var o = event.target; alert(o.previousElementSibling.innerHTML); }
18th Mar 2019, 9:52 AM
Ipang
+ 7
Try to make the classes different so you can differentiate between the divs😉 / ithink it will help you😉
18th Mar 2019, 7:32 AM
Max Andal
Max Andal - avatar
+ 3
Fury you're welcome, I'm glad if it helps 👍
18th Mar 2019, 3:59 PM
Ipang
+ 2
Fury, why don't you share your code link on your question? actually it will help others to understand the problem better by seeing your code. How do you choose the div from the many divs? do you choose in random? or is there some criteria that defines which div should be selected? My idea would be to use `getElementsByClassName` or `querySelectorAll` method to collect all the divs having similar class name, and once we have it - we can choose by index. Just an idea : )
18th Mar 2019, 7:44 AM
Ipang
+ 2
lpang ,thank you your code works perfectly
18th Mar 2019, 12:06 PM
Fury
+ 1
Try to make the classes different so you can differentiate between the divs😉 / ithink it will help you😉 no I cant make diffrent class names i'v written about it
18th Mar 2019, 9:05 AM
Fury