I need to combine all variables in one object:
$.get("https://www.virginmegastore.ae/en/gaming/playstation/playstation-games/sonic-forces---ps4/p/714792", function (data) {
var productNamePost=$("[name='productNamePost']").val();
var productCodePost=$("[name='productCodePost']").val();
var bg = $('.pdp_image-carousel-image.js-zoomImage-mobile').css('background-image');
var productPrice=$(".price__container > .price__value > .price__number").text();
var url = window.location.href;
console.log(url);
});
Sorry the question was unclear,i was need this
let obj = Object.assign({},var)
I also need to run the code when the button clicked
$.get("https://www.virginmegastore.ae/en/gaming/playstation/playstation-games/sonic-forces---ps4/p/714792", function (data) {
$("#addToCartForm > #addToCartButton").click(function(){
var productNamePost=$("[name='productNamePost']").val();
var productCodePost=$("[name='productCodePost']").val();
var bg = $('.pdp_image-carousel-image.js-zoomImage-mobile').css('background-image');
var productPrice=$(".price__container > .price__value > .price__number").text();
var url = window.location.href;
let obj = Object.assign({}, [productNamePost, productCodePost, bg, productPrice, url]);
console.log(obj)});
});
but i got below error when i clicked the button:
Try this:
let obj = Object.assign({}, [
productNamePost: $("[name='productNamePost']").val(),
productCodePost: $("[name='productCodePost']").val(),
bg: $('.pdp_image-carousel-image.js-zoomImage-mobile').css('background-image'),
productPrice: $(".price__container > .price__value > .price__number").text(),
url: window.location.href
]);
This will create an object array with all your variables. Read more about Object.assign()
here
And what's the problem?
const object = {
productNamePost: $("[name='productNamePost']").val(),
productCodePost: $("[name='productCodePost']").val(),
bg: $('.pdp_image-carousel-image.js-zoomImage-mobile').css('background-image'),
productPrice: $(".price__container > .price__value > .price__number").text(),
url: window.location.href,
}
console.log(object.url); // and use
Run to read .
Like this?
var newObj = {
"productNamePost": $("[name='productNamePost']").val(),
"productCodePost": $("[name='productCodePost']").val(),
"variable_name": variable_value
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.