I have input boxes ID labeled with the prefix "add-product-" and I want to replace the "add-product-" with nothing. So I tried using data[field.id].replace("add-product-", "");
but it gives me the error: [Error] TypeError: undefined is not an object (evaluating 'data[field.id].replace')
Here is my code:
function addproduct(){
var datafeilds = Array.from(document.querySelectorAll("[id^='add-product-']"));
var data = {};
datafeilds.forEach(function(field){
data[field.id].replace("add-product-", "");
data[field.id] = field.value;
});
console.log(data);
}
The output is:
[Log] {add-product-name: "", add-product-overlay1: "", add-product-overlay2: "", add-product-wholesale_price: "", add-product-delivery_price: "", …}
I want it to be:
[Log] {name: "", overlay1: "", overlay2: "", wholesale_price: "", delivery_price: "", …}