Jump to content

Dropdown box value is not updating when changed using jQuery

Go to solution Solved by mcbowen,

Recommended Posts

Posted (edited)

Site URL: https://pigeon-eagle-lfzb.squarespace.com/store-1/p/style-01-hs2wj-rec4m-3dpak

Password = 123

Hi, 

When I try to change the value set in a select element using jQuery the value does not display on the screen, the selected attribute is added in HTML and when clicking on the dropdown the correct value is highlighted.

This is what displays:

image.png.16804829c0bc2e48f71fc948df9ce8ca.png

This is what I am expecting:

image.png.b0d6cff1f7b9efd67305f8b57de35986.png

The correct value is highlighted when clicking on the dropdown:

image.png.3b02a7d8c932a16ff85dc98722508f9a.png

I have the following code which is not working on the Squarespace site:

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
  $(document).ready(function() {
    $('select[data-variant-option-name=Color] option[value=Purple]').attr('selected', 'selected');
  });
</script>

I've copied the except HTML and jQuery into jsfiddle and it seems to work there:

http://jsfiddle.net/qaft4ve0/1/

I have also tried using .val and .props but this does not fix it.

Updates:

I have since tried adding a dropdown box on the home page and this works correctly so there must be something on the product page which is causing the issue.

I have a few more updates on this (Still not working):

Ive manaaged to get the dropbown box to visually update by setting the data-text attribute:

$("div.variant-select-wrapper.color-wrapper").attr("data-text", "white");

Looking at the HTML code I have noticed the following:

data-unselected-options="["Color","Size"]"

This does not change when using jQuery, it only changes when I manually click on a dropdown box and select it.

There is also a new attribute data-selected-variant that gets added when both colour and size are selected:

data-selected-variant="{"attributes":{"Size":"M","Color":"White"},"optionValues":[
{"optionName":"Size","value":"M"},{"optionName":"Color","value":"White"}],
"id":"ae248023-a7b2-4ebb-89ef-55764797f7e1","sku":"SQ2275958","price":4000,
"salePrice":0,"priceMoney":{"currency":"GBP","value":"40.00"},
"salePriceMoney":{"currency":"GBP","value":"0.00"},"onSale":false,"unlimited":true,
"qtyInStock":0,"width":0,"height":0,"weight":0,"imageIds":[],"images":[],"len":0}"

and also:

data-variant-in-stock="true"

 

Edited by mcbowen
updates
  • mcbowen changed the title to Dropdown box value is not updating when changed using jQuery
  • Replies 1
  • Views 4.4k
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

  • Solution
Posted

Adding the below code triggered the change event and seems to be working for me

document.querySelector("select[data-variant-option-name='Color']").dispatchEvent(new Event("change"));

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.