I am working on a Shopify store where there are multiple divs like below:
<div class="options-selection__option-values" data-variant-option="" data-variant-option-index="0" data-variant-option-chosen-value="One"></div>
<div class="options-selection__option-values" data-variant-option="" data-variant-option-index="0" data-variant-option-chosen-value="Two">
<div class="options-selection__option-values" data-variant-option="" data-variant-option-index="0" data-variant-option-chosen-value="Three">
<div class="options-selection__option-values" data-variant-option="" data-variant-option-index="0" data-variant-option-chosen-value="Four">
I want to get the value of custom attribute data-variant-option-chosen-value
and print that into <span class="selected-variant"></span>
I tried reading the custom attribute value by following jQuery without any success
$(document).ready(function() {
$('.options-selection__option-values').each(function() {
console.log($(this).attr('data-variant-option-chosen-value'));
});
2
Answers
It works just fine, you just have syntax error, you are missing "})" at the end and it throws the error
Uncaught SyntaxError: Unexpected end of input"
As of now, you are not closing neither the anonymous callback function, neither the ready function.
Get the value of attribute using
$(this).attr("data-variant-option-chosen-value")
, concatenate it, set it to the required target.Also your template and script has some errors. divs missing closing tag and the
$(document).ready(function () {
missing the cloaing tag