0
So I have a function in javascript that changes the class of an object and then later I try to look for that object in another function with the new class , but that doesn’t happen and gives me a Undefined object .. I would like to know what was wrong if it was not too inconvenient, besides that also someone could give me some hint about , when I use the localstorage if it is possible to keep adding a higher key value than I already have ? Thank you!
Html:
<button id="xs" onclick="SizeButtonStyle(this)" class="unclicked_size_button"><b>XS</b></button>
<button id="s" onclick="SizeButtonStyle(this)" class="unclicked_size_button"><b>S</b></button>
<button id="m" onclick="SizeButtonStyle(this)" class="unclicked_size_button"><b>M</b></button>
<button id="l" onclick="SizeButtonStyle(this)" class="unclicked_size_button"><b>L</b></button>
<button id="xl" onclick="SizeButtonStyle(this)" class="unclicked_size_button"><b>XL</b></button>
Javascript:
function SizeButtonStyle(el) {
var size = el
if(el.id == 'xs'){
document.getElementById('xs').className = "clicked_size_button";
document.getElementById('s').className = "unclicked_size_button";
document.getElementById('m').className = "unclicked_size_button";
document.getElementById('l').className = "unclicked_size_button";
document.getElementById('xl').className = "unclicked_size_button";
}
if(el.id == 's'){
document.getElementById('xs').className = "unclicked_size_button";
document.getElementById('s').className = "clicked_size_button";
document.getElementById('m').className = "unclicked_size_button";
document.getElementById('l').className = "unclicked_size_button";
document.getElementById('xl').className = "unclicked_size_button";
}
if(el.id == 'm'){
document.getElementById('xs').className = "unclicked_size_button";
document.getElementById('s').className = "unclicked_size_button";
document.getElementById('m').className = "clicked_size_button";
document.getElementById('l').className = "unclicked_size_button";
document.getElementById('xl').className = "unclicked_size_button";
}
if(el.id == 'l'){
document.getElementById('xs').className = "unclicked_size_button";
document.getElementById('s').className = "unclicked_size_button";
document.getElementById('m').className = "unclicked_size_button";
document.getElementById('l').className = "clicked_size_button";
document.getElementById('xl').className = "unclicked_size_button";
}
if(el.id == 'xl'){
document.getElementById('xs').className = "unclicked_size_button";
document.getElementById('s').className = "unclicked_size_button";
document.getElementById('m').className = "unclicked_size_button";
document.getElementById('l').className = "unclicked_size_button";
document.getElementById('xl').className = "clicked_size_button";
}
}
function addToCartClicked(event){
var product = document.getElementsByClassName('product_name')[0].innerText
var price = document.getElementsByClassName('price')[0].innerText
var size = document.getElementById('clicked_size_button')[0].innerText
var info = [product,size,price];
localStorage.setItem(1, JSON.stringify(info));
}
You can display the HTML (or function) that calls
addToCartClicked
? I would like to add an answer with an example.– Sergio