Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  122] [ 5]  / answers: 1 / hits: 15574  / 7 Years ago, sat, october 21, 2017, 12:00:00

I have the following elements in a html document



<div id=u11 class=ax_default image data-label=image1>
<img id=u11_img class=img src=images/image1_u11.png>
</div>

<div id=u23 class=ax_default image data-label=image2>
<img id=u23_img class=img src=images/image2_u23.png>
</div>


I want to write a javascript onclick function for another element that set image2 src to image1 src



The problem is that ids are random so I cannot use them but fortunately I can use the data-label to get the external div objects with $('[data-label=image1]')



¿How can I set the src in the inner img?


More From » html

 Answers
3

You can use the following



var image1 = document.querySelector('[data-label=image1] img'),
image2 = document.querySelector('[data-label=image2] img');

document.querySelector('#button').addEventListener('click', function(){
image2.src = image1.src;
});

[#56160] Thursday, October 19, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aubriechantalr

Total Points: 380
Total Questions: 95
Total Answers: 86

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
;