My code looks like this:
.modelColors {
width: 100%;
height: 100vh;
position: relative
}
.modelColors .leftCont {
flex: 0 0 50%;
transition: all .3s ease-in-out;
transition: var(--anim300);
overflow: hidden
}
.modelColors .leftCont .lead {
width: 100%;
max-width: 600px;
display: block
}
.modelColors .leftCont .lead span {
font-size: 6vw;
font-family: "suzukiproheadline";
font-family: var(--headline);
line-height: 130px;
color: rgba(0,0,0,.3);
color: var(--blackAlpha30)
}
.modelColors .rightCont {
flex: 0 0 50%;
padding: 50px
}
.modelColors .rightCont .rightHolder {
width: 100%;
max-width: 80%
}
.modelColors .rightCont .rightHolder .modelColoImg img {
width: 100%;
max-width: 42px;
margin: 0 15px 10px 0;
transition: all .2s ease-in-out;
transition: var(--anim200);
border: 2px solid #eaeaea;
border-radius: 50%;
cursor: pointer
}
.modelColors .rightCont .rightHolder .modelColoImg img:hover {
transform: scale(1.4);
transition: all .2s ease-in-out;
transition: var(--anim200)
}
.modelColors .rightCont .rightHolder .modelTopCont {
width: 100%
}
.modelColors .rightCont .rightHolder .modelTopCont .modelBtn {
background-color: hsla(0,0%,100%,.25);
background-color: var(--whiteAlpha025);
color: #202123;
color: var(--black);
display: flex;
align-items: center;
justify-content: flex-start;
border-radius: 28px;
padding: 10px 20px;
height: 44px;
margin: 0 20px 0 0;
border: 1px solid #202123;
border: 1px solid var(--black);
transition: all .2s ease-in-out;
transition: var(--anim200);
font-family: "suzukiproheadline";
font-family: var(--headline)
}
.modelColors .rightCont .rightHolder .modelTopCont .modelBtn img {
margin: 0 10px 0 0
}
.modelColors .rightCont .rightHolder .modelTopCont .modelBtn:hover {
transition: all .2s ease-in-out;
transition: var(--anim200)
}
.modelColors .rightCont .rightHolder .modelTopCont .modelBtn.noBg,.modelColors .rightCont .rightHolder .modelTopCont .modelBtn:hover {
background-color: transparent;
border: 1px solid hsla(0,0%,100%,.7);
border: 1px solid var(--whiteAlpha07)
}
.modelColors .rightCont .rightHolder .modelTopCont .modelBtn.noBg:hover {
background-color: hsla(0,0%,100%,.25);
background-color: var(--whiteAlpha025);
transition: all .2s ease-in-out;
transition: var(--anim200);
border: 1px solid transparent
}
.modelColors .modelMainImage {
width: 100%;
height: 100vh;
position: absolute;
left: 0;
top: 10%;
display: flex;
align-items: center;
justify-content: center;
pointer-events: none
}
.el-tooltip__popper.is-light {
box-shadow: 0 2px 10px rgba(0,0,0,.2);
border: 1px solid #e5e5e5;
background-color: #fff;
font-family: "suzukiproregular";
font-family: var(--regular);
font-size: 18px;
color: #202123;
color: var(--black)
}
.el-tooltip__popper.is-light .popper__arrow {
border-color: transparent;
border-bottom-color: transparent!important
}
.el-tooltip__popper.is-light .popper__arrow:after {
border-bottom-color: #fff!important
}
.modelColors .leftCont .lead span {
font-size: 6vw;
font-family: "suzukiproheadline";
line-height: 130px;
color: rgba(0,0,0,.3);
}
<section id="model-colors" class="modelColors d-flex">
<div class="leftCont d-flex align-items-center justify-content-center" style="background-color:#D31316;">
<div class="lead"><span>Burning Red Pearl Metallic</span></div>
</div>
<div class="rightCont d-flex justify-content-center">
<div class="rightHolder">
<div class="modelTopCont d-flex align-items-center justify-content-between">
<h2>Colors</h2>
</div>
<div class="modelColoImg d-flex align-items-center justify-content-start flex-wrap">
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/burning-red.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-8288" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/speedy-blue.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-6384" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/pure-white.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-9890" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/premium-silver.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-1345" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/mineral-gray.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-5504" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/super-black.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-7421" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/fervent-red.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-9427" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/red-black.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-6672" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/orange-black.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-7222" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/yellow-gray.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-7131" tabindex="0"></div>
<div><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/blue-black.png" class="el-tooltip img-responsive item s16" aria-describedby="el-tooltip-5123" tabindex="0"></div>
</div>
</div>
</div>
<div class="modelMainImage"><img id="changeOne" src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/red.png" alt="" class="img-responsive"></div>
<div class="d-none"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/red.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/blue.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/silver.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/sliver.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/gray.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/black.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/red-2.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/black-and-red.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/orange-and-black.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/yellow-black.png"><img src="https://suzuki.bizmo.al/wp-content/uploads/2023/03/blue-black-1.png"></div>
</section>
so, what i want to do is to be able to click on the colors on the right and change the background of the div on the right and the car color (replace url).
I was able to change color with javascript but i couldn’t change the url of the car bellow.
How should I approach this?
2
Answers
Try adding a class on image parent div and onclick on loop,
I’ve revised the previous answer, and taken a different approach; given that there’s more data to store I chose not to continue littering the DOM with further custom attributes – though this is a valid, and perfectly acceptable, method – and instead opted to use a JavaScript Object to cache the relevant data together, as below.
Explanatory comments are in the code to try make sense of my approach, though I’ll note in advance that I’ve stripped away rather a lot of your HTML that seemed unnecessary (this may, or may not be unnecessary, but you’ll have to decide for yourself):
JS Fiddle demo
References:
Array.prototype.map()
.Array.prototype.slice()
.Array.prototype.join()
.document.querySelector()
.document.querySelectorAll()
.Element.querySelector()
.Element.querySelectorAll()
.Event
.Event.currentTarget()
.EventTarget.addEventListener()
.String.prototype.replace()
.String.prototype.split()
.String.prototype.toUpperCase()
.