My background: I learned CSS & HTML a few years ago and I am learning Javascript & JQuery. Currently, I've got to improve the e-commerce website of my company.
Here's what I am trying to do.
I've got a product page where there is a picture of a product (inside a window) and some colour buttons.
When you click on a colour button, it gets selected but the photo doesn't change.
You probably guessed it, I want to add code that shows the picture of the product when you click/select the colour that matches it.
I wrote a JS code and my idea behind it, is to isolate the url of the picture and to change it by the one corresponding to the colour.
The problem is that I get this error: Uncaught TypeError: Cannot read property 'split' of undefined.
I searched for solutions but none of them seem to correspond to my problem. Thus, I need help.
Here's the HTML (the window/block showing the picture of the product):
<div
style="z-index: 999;
overflow: hidden;
margin-left: 0px; margin-top: 0px;
background-position: -144px 0px;
width: 456px; height: 343px;
float: left;
cursor: crosshair;
background-repeat: no-repeat; position: absolute;
background-image: url("https://be-goji.com/wp-
content/uploads/2017/01/OSSO-ROSE.jpg");
top: 0px; left: 0px; display: none;"
class="zoomWindow">
</div>
Here's a snippet of a Javascript code that I wrote in order to fix it (with comments):
// isolates the HTML from the window
var colorSwitch = document.getElementsByClassName('zoomWindow');
// splits the code in several strings in an array
var colorSwitchUrl = colorSwitch[0].split(';')
// extracts the string I want to change
var colorSwitchChange = colorSwitchUrl.slice(12,13);
// gets the element I want to put the event onto
var chooseColorNoir = document.getElementsByTagName('label');
//function that brings picture to show in the window on click
chooseColorNoir[0].onclick = function(){
// makes the string disappear
colorSwitchChange.pop();
// replaces it with a good one
colorSwitchChange.push("https://be-goji.com/wp-
content/uploads/2017/01/coco_noir.jpg");
};
Could you help me find where the problem lies? Do you have another method that you recommend?
Thanks a million!
Nat
Note: I have got to call the picture with an url since it isn't available on the page.