-2

Есть такой селект, как при клике на него

<label class="b__variant selected">
    <input type="hidden" name="t_id" value="1">
    <input type="hidden" name="v_id" value="2">
</label>

передать значение в дата-атрибут другой кнопке

<div class="b__event">
    <a href="#" data-t="1" data-v="1">Кнопка</a>
</div>

1 ответ 1

2

Это нужно было ?

function onclick (e){
  if (e) {
    var el = e.target || window.Event;
    var a = document.querySelector('.b__event a');
    el = el.children;
    a.dataset.t = el[0].value;
    a.dataset.v = el[1].value;
    console.log(a);
  }
}

[].forEach.call(document.querySelectorAll('.b__variant'), function (item) {
    if (window.addEventListener) {
        item.addEventListener('click', onclick)
    } else {
        item.onclick = onclick;
    }
})
.b__variant {
  background:#dfdfdf;
  width:50px; 
  height:50px;
  display:inline-block;
}

.b__variant:hover {
  background:#c00;
}
<label class="b__variant selected">
    <input type="hidden" name="t_id" value="1">
    <input type="hidden" name="v_id" value="2">
</label>

<label class="b__variant selected">
    <input type="hidden" name="t_id" value="2">
    <input type="hidden" name="v_id" value="4">
</label>
<label class="b__variant selected">
    <input type="hidden" name="t_id" value="1">
    <input type="hidden" name="v_id" value="5">
</label>
<div class="b__event">
    <a href="#" data-t="1" data-v="1">Кнопка</a>
</div>

1
  • спасибо вам большое
    – Ivan
    5 авг 2020 в 17:54

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.