<select name="variant" class="fn_variant variant_select {if $product->variants|count == 1}hidden{/if}" id="myselect">
{foreach $product->variants as $v}
<option for="{if $v->weight}{$v->weight}{/if}" value="{$v->id}" data-price="{$v->price|convert}" data-stock="{$v->stock}"{if $v->compare_price > 0} data-cprice="{$v->compare_price|convert}"{/if}{if $v->sku} data-sku="{$v->sku|escape}"{/if}>{if $v->name}{$v->name|escape}{else}{$product->name|escape}{/if}</option>
{/foreach}
</select>
document.getElementById("myselect").addEventListener("change", function () {
document.getElementById('mydiv').innerHTML = "Вес брутто*: " + this.value;
});
document.getElementById("myselect").addEventListener("change",
function () {
let opt;
for(let i=0;i<this.children.length;i++){
let child = this.children[i];
if(this.value == child.value) {
opt = child;
break;
}
}
if(!opt)return;
let for_attr = opt.getAttribute("data-price") || "???";
document.getElementById('mydiv').innerHTML = "Вес брутто*: "
+ for_attr;
}
);
document.querySelector('#myselect').addEventListener('change', function(e) {
const select = this;
// или
// const select = e.target;
// const select = e.currentTarget;
const [ option ] = select.selectedOptions;
// или
// const option = select[select.selectedIndex];
// const option = select.querySelector(`[value="${select.value}"]`);
// const option = select.querySelector('option:checked');
// const option = [...select.children].find(n => n.selected);
const forAttr = option.getAttribute('for');
// или
// const forAttr = option.attributes.for.value;
document.querySelector('#mydiv').innerText = `Вес брутто*: ${forAttr}`;
});