I'm trying to replace the old img.src with the value (new img.src) from input fields with Arrays.
The problem I'm stuck at is: the value in the arrays is replaced with the new value but the img.src inside HTML content does not change, so the img stay the same.
function changegallery() {
var newimgURL = Array.from(document.querySelectorAll('#menuitems > input')).map(imgdata => imgdata.value);
console.log('newimgURL', newimgURL);
var oldimgURL = Array.from(document.querySelectorAll('#gallery > a > img')).map(imgdata => imgdata.src);
var changeimgURL = oldimgURL.splice(0, oldimgURL.length, ...newimgURL);
console.log('IMG', oldimgURL);
};
<form id="menuitems">
<input type="url" id="changeimg1" placeholder="URL 1" />
<input type="url" id="changeimg2" placeholder="URL 2" />
<input type="url" id="changeimg3" placeholder="URL 3" />
<input type="url" id="changeimg4" placeholder="URL 4" />
<Button onClick={changegallery()}>button</Button>
</form>
<div id="gallery">
<a> <img id="img1" src="https://via.placeholder.com/150"> </a>
<a> <img id="img2" src="https://via.placeholder.com/150"> </a>
<a> <img id="img3" src="https://via.placeholder.com/150"> </a>
<a> <img id="img4" src="https://via.placeholder.com/150"> </a>
</div>
fiddle: https://jsfiddle.net/cn1L4y7j/
When you create a second array containing the original image src attributes and then modify that array to contain the new values -- these don't stay as references to the original DOM attributes, it's just an array of strings, so modifying it won't affect the page. Instead you need to explicitly insert those new values into the DOM.