I made this code so that when you click one of the nine images, you get another one (I made images with text on Photoshop). My code works fine but it is just really long. Does anyone know how to make it shorter?
Javascript:
$(document).ready(function() { /*Altijd aanroepen per script*/
$(document)
.on('click', '#imgClickAndChange', function changeImage() {
if (document.getElementById("imgClickAndChange").src == "http://127.0.0.1:50150/img/thirdtwo.png") {
document.getElementById("imgClickAndChange").src = "http://127.0.0.1:50150/img/thirdtwohover.png";
} else {
document.getElementById("imgClickAndChange").src = "http://127.0.0.1:50150/img/thirdtwo.png";
}
})
$(document)
.on('click', '#imgClickAndChange2', function changeImage() {
if (document.getElementById("imgClickAndChange2").src == "http://127.0.0.1:50150/img/thirdone.png") {
document.getElementById("imgClickAndChange2").src = "http://127.0.0.1:50150/img/thirdonehover.png";
} else {
document.getElementById("imgClickAndChange2").src = "http://127.0.0.1:50150/img/thirdone.png";
}
})
$(document)
.on('click', '#imgClickAndChange3', function changeImage() {
if (document.getElementById("imgClickAndChange3").src == "http://127.0.0.1:50150/img/thirdthree.png") {
document.getElementById("imgClickAndChange3").src = "http://127.0.0.1:50150/img/thirdthreehover.png";
} else {
document.getElementById("imgClickAndChange3").src = "http://127.0.0.1:50150/img/thirdthree.png";
}
})
$(document)
.on('click', '#imgClickAndChange4', function changeImage() {
if (document.getElementById("imgClickAndChange4").src == "http://127.0.0.1:50150/img/thirdfour.png") {
document.getElementById("imgClickAndChange4").src = "http://127.0.0.1:50150/img/thirdfourhover.png";
} else {
document.getElementById("imgClickAndChange4").src = "http://127.0.0.1:50150/img/thirdfour.png";
}
})
$(document)
.on('click', '#imgClickAndChange5', function changeImage() {
if (document.getElementById("imgClickAndChange5").src == "http://127.0.0.1:50150/img/thirdfive.png") {
document.getElementById("imgClickAndChange5").src = "http://127.0.0.1:50150/img/thirdfivehover.png";
} else {
document.getElementById("imgClickAndChange5").src = "http://127.0.0.1:50150/img/thirdfive.png";
}
})
$(document)
.on('click', '#imgClickAndChange6', function changeImage() {
if (document.getElementById("imgClickAndChange6").src == "http://127.0.0.1:50150/img/thirdsix.png") {
document.getElementById("imgClickAndChange6").src = "http://127.0.0.1:50150/img/thirdsixhover.png";
} else {
document.getElementById("imgClickAndChange6").src = "http://127.0.0.1:50150/img/thirdsix.png";
}
})
$(document)
.on('click', '#imgClickAndChange7', function changeImage() {
if (document.getElementById("imgClickAndChange7").src == "http://127.0.0.1:50150/img/thirdseven.png") {
document.getElementById("imgClickAndChange7").src = "http://127.0.0.1:50150/img/thirdsevenhover.png";
} else {
document.getElementById("imgClickAndChange7").src = "http://127.0.0.1:50150/img/thirdseven.png";
}
})
$(document)
.on('click', '#imgClickAndChange8', function changeImage() {
if (document.getElementById("imgClickAndChange8").src == "http://127.0.0.1:50150/img/thirdeight.png") {
document.getElementById("imgClickAndChange8").src = "http://127.0.0.1:50150/img/thirdeighthover.png";
} else {
document.getElementById("imgClickAndChange8").src = "http://127.0.0.1:50150/img/thirdeight.png";
}
})
$(document)
.on('click', '#imgClickAndChange9', function changeImage() {
if (document.getElementById("imgClickAndChange9").src == "http://127.0.0.1:50150/img/thirdnine.png") {
document.getElementById("imgClickAndChange9").src = "http://127.0.0.1:50150/img/thirdninehover.png";
} else {
document.getElementById("imgClickAndChange9").src = "http://127.0.0.1:50150/img/thirdnine.png";
}
})
});
CSS:
#pictures {
background-color: rgb(35, 35, 35);
color: rgb(204, 55, 77);
text-align: center;
padding-top: 2%;
padding-bottom: 2%;
width: 100%;
min-height: 100vh;
float: left;
overflow: hidden;
font-size: 30px;
}
.onethird {
margin-left: 1%;
margin-right: 1%;
margin-top: 5%;
width: 25%;
}
<div id="pictures">
<img src="img/thirdtwo.png" class="onethird" id="imgClickAndChange" onclick="changeImage()"></img>
<img src="img/thirdone.png" class="onethird" id="imgClickAndChange2" onclick="changeImage()"></img>
<img src="img/thirdthree.png" class="onethird" id="imgClickAndChange3" onclick="changeImage()"></img>
<img src="img/thirdfour.png" class="onethird" id="imgClickAndChange4" onclick="changeImage()"></img>
<img src="img/thirdfive.png" class="onethird" id="imgClickAndChange5" onclick="changeImage()"></img>
<img src="img/thirdsix.png" class="onethird" id="imgClickAndChange6" onclick="changeImage()"></img>
<img src="img/thirdseven.png" class="onethird" id="imgClickAndChange7" onclick="changeImage()"></img>
<img src="img/thirdeight.png" class="onethird" id="imgClickAndChange8" onclick="changeImage()"></img>
<img src="img/thirdnine.png" class="onethird" id="imgClickAndChange9" onclick="changeImage()"></img>
</div>
</div>
5
Answers
Remove
onclick
attributes, as you don’t need them (and it’s not really good practice to use them).click
event handlers are set in yur JS code.This should do the job for your HTML example:
I would recommend two things. First put both your urls on each of your elements.
Then when they all follow that pattern you could do something like:
Try this..
Unfortunarely I already started working on a solution for you before I saw others posted, oh well! Here is what I wrote up for you.
I went with one general
click
event listener for a generic class you can apply to all your images you want to swap when clicked. When an image is clicked, it will swap images with names you defined using thedata-
attribute available onHTML
elements. I am using the jQuery.attr()
function because I do not know which jQuery version you’re using and.data()
isn’t available for earlier versions of jQuery. Please let me know if this works out for you, or if I need to make adjustments. Code is posted below, cheers!JavaScript
HTML