skip to Main Content

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:

enter image description here

2

Answers


  1. 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

    Login or Signup to reply.
  2. Like this?

    var newObj = {
      "productNamePost": $("[name='productNamePost']").val(),
      "productCodePost": $("[name='productCodePost']").val(),
      "variable_name": variable_value
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search