Can't use variables with Ajax Cart Update

Hello everyone,

i am having troubles using the Ajax Cart Update Function.
When hardcoding the variant_id, everything works but when i pack the variant_id into a variable it “cannot find variant”. (Ajax Cart Add is working like this)
The variable does contain the same id.
What did I do wrong ?

<button onclick="addToCart({{ variant.id }})">Add</button>

function addToCart(test) { 
    
    jQuery.post('/cart/update.js', {
      updates: {
        test: 1}
    });	   
  }

Be sure to check your browsers developer tools console first for other errors.

Use a debugger keyword in the addToCart function to jump into it when it’s called and check that an ID is being passed in then if not verify if the variant.id is being rendered to the html.

If it’s not being rendered with {{ variant.id}} that indicates your not in a variant loop

@PaulNewton

It selected the correct VariantID, still it cannot find the variant

Christopher99_1-1608652991592.png

If I hardcode the value it works though.

Strange without being able to see it, only thing left is to double check that

it is variant Id and not the Product ID

in the javascript see if it’s an INT or a String

Examine the network request, parameters that get sent

Hello @Christopher99 @PaulNewton ,

Hello @Christopher99 , @diego_ezfy I also faced the same problem while updating the cart with dynamic js variable.
But I used this method and problem get resolved.
jQuery.post(‘/cart/update.js’,
“updates[794864053]=2&updates[794864233]=3”
);
You can also try this.
My js code is this:-
$.post(‘/cart/update.js’,
“updates[”+donation_id+“]=2”
);
Where donation id i am getting as js variable.
See the screenshot of my payload over browser.