I'm not sure how to use event target closest([name="..."])
as I understand closest()
should get the element after I change the value in the input field to calculate, but the elements I get are null
function calculate(e) {
if (e.target.name == 'qty') {
var elemPrice = e.target.closest('[name="price"]') //***
var elemAmount = e.target.closest('[name="amount"]') //***
console.log(elemPrice)
console.log(elemAmount)
elemAmount.value = elemPrice.value * e.target.value
}
}
document.addEventListener('DOMContentLoaded',function(){
document.getElementsByName('qty').forEach(t => { t.addEventListener('change',calculate) })
})
<table>
<tbody>
<tr class="uk-hidden">
<td>1</td>
<td><input name="price"></td>
<td><input name="qty"></td>
<td><input name="amount"></td>
</tr>
<tr class="uk-hidden">
<td>2</td>
<td><input name="price"></td>
<td><input name="qty"></td>
<td><input name="amount"></td>
</tr>
<tr class="uk-hidden">
<td>3</td>
<td><input name="price"></td>
<td><input name="qty"></td>
<td><input name="amount"></td>
</tr>
</tbody>
</table>
I've tried
...
var elemPrice = e.target.parentElement.previousElementSibling.lastElementChild //***
var elemAmount = e.target.parentElement.nextElementSibling.lastElementChild //***
...
but I want to do like closest
Is there a way to do this?
anyways Thank you in advance