I'm doing a JavaScript function which is to hover anyone of the <a>
elements and change the <img>
src and <p>
accordingly.
I am practicing and learning JavaScript. So I have create a super simple JavaScript function code.
function onHover() {
var pistiProducts = [
"https://temp1.asign.pro/wp-content/uploads/2019/05/test-1.jpg",
"https://temp1.asign.pro/wp-content/uploads/2019/05/test-2.jpg"
];
var replace = document.getElementById("replacement");
var itemdec = document.getElementById("text-replacement");
replace.src = pistiProducts[1];
itemdec.innerHTML = "Test 1";
}
In my HTML:
<div class="dropdown-menu-wrapper d-flex">
<div class="dropdown-item-wrapper flex-grow-1">
<a id="pisti-products" onmouseover="onHover();" class="pisti-products" href="#">Action</a>
<a class="" href="#">Another action</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
<a class="" href="#">Something else here</a>
</div>
<div class="dropdown-display-wrapper">
<div class="display-img replacement">
<img id="replacement" class="w-100 img-fluid" src="https://temp1.asign.pro/wp-content/uploads/2019/05/test-1.jpg" alt="">
</div>
<p class="mb-0 w-100 pisti-title" id="text-replacement">Another action</p>
</div>
</div>
Currently what I will do is I will write 13 function and set the onmouseover=""
to the relevant one. But I don't think this is a best solution because I have more to do something like this.
As you can see my JavaScript code included Array
. But I have no idea what can be done with array. :(
I need help to suggest me the best and easy solution. Please suggest me in JavaScript code instead of jQuery. I'm not good with basic JavaScript syntax and structure.