Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
how to add string in dynamic array

Here i got only id how can I modify to get (37398351478977: 1 ,42110385783015:1) string so i can add this in ajax for add to cart

 var values=[];

    $(".ex_product_select").change(function(){
    if($(this).is(":checked"))
      values.push($(this).val());
    else{
      var x = values.indexOf($(this).val());
      values.splice(x,1) ;
    }
    $(".hide_extra_variant").text(values);
    });
 $(document).on("click",".add",function(){ 
   jQuery.post('/cart/update.js', {  updates: {  values } });
  });
<script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>   

 Product1 : <input type="checkbox" class="ex_product_select" style="display:inline-block;" name="exselect" value="37398331392193">
        Product2 : <input type="checkbox" class="ex_product_select" style="display:inline-block;" name="exselect" value="37398351478977">
        Product3 : <input type="checkbox" class="ex_product_select" style="display:inline-block;" name="exselect" value="42110385783015">

<button class="add" value="add_to_cart">Add To Cart</button>

     <p class="hide_extra_variant"></p>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Do you want this result?

var values=[];

    $(".ex_product_select").change(function(){
    if($(this).is(":checked"))
      values.push({key:values.length+1,value:$(this).val()});
    else{
      var x = values.findIndex(x => x.value == $(this).val());
      values.splice(x,1) ;
    }
    
    console.clear();
    
    console.log(values)
    
    let allValues = "(" + values.map(x => x.value + ":1").join() + ")";
    
    $(".hide_extra_variant").text(allValues);
    });
 $(document).on("click",".add",function(){ 
   jQuery.post('/cart/update.js', {  updates: {  values } });
  });
<script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>   

 Product1 : <input type="checkbox" class="ex_product_select" style="display:inline-block;" name="exselect" value="37398331392193">
        Product2 : <input type="checkbox" class="ex_product_select" style="display:inline-block;" name="exselect" value="37398351478977">
        Product3 : <input type="checkbox" class="ex_product_select" style="display:inline-block;" name="exselect" value="42110385783015">

<button class="add" value="add_to_cart">Add To Cart</button>

     <p class="hide_extra_variant"></p>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!