0
I’m new to javascript, I would like help with a problem.
I managed to make sure that when selecting the value of the first select of "UNIT" the bottom select came with the same value, but as you can see in the image it is as required yet and does not enable the field to finalize the purchase. How can I solve this problem?
Note: follows the JS/TS code used:
class UnitSelection {
get targets(): NodeListOf<HTMLSelectElement> {
return document.querySelectorAll('[data-option-id]');
}
init(): void {
this.attachListeners();
}
attachListeners(): void {
this.targets.forEach(option => {
option.addEventListener('change', (e: Event) =>
this.handleChange(e)
);
});
}
handleChange(e: Event): void {
const target: EventTarget | null | HTMLSelectElement = e.currentTarget;
if (!(target instanceof HTMLSelectElement)) {
return;
}
const value = target.value;
this.targets.forEach(target => {
if (target.value == '') {
target.value = value;
}
});
}} const unitSelection = new UnitSelection(); unitSelection.init();
HTML:
<select name="place" location-select data-item-id="<?php echo $_item->getId(); ?>" data-product-id="<?php echo $_item->getProductId(); ?>" data-option-id="<?php echo $optionId; ?>">
<?php if ($placeList) : ?>
<?php if (is_string($placeList)) : ?>
<option value="1"><?php echo $placeList ?></option>
<?php else : ?>
<option value="">Selecione...</option>
<?php foreach ($placeList as $key => $place) : ?>
<?php $_formatedOptionValue = $this->getFormatedOptionValue($place) ?>
<?php if ($_formatedOptionValue['value'] === $selectedOption) : ?>
<option selected="selected" value="<?php echo $key; ?>">
<?php echo $_formatedOptionValue['value'] ?>
</option>
<?php else : ?>
<option value="<?php echo $key; ?>">
<?php echo $_formatedOptionValue['value'] ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php else : ?>
<option value="1">Unidade Padrão</option>
<?php endif; ?>
</select>
I appreciate the help ;)
If using jQuery, use the method .one
– Sam
By the explanation of the question, you do not want to change all the selects, you want to give in only one and change the value of the others.
– Sam
@Sam this, I want when selecting a unit in select from above all others also be changed to the same value.
– Rafael de Oliveira Silva
Only on top? You’re using jQuery?
– Sam
@Sam no, I’m starting my studies yet
– Rafael de Oliveira Silva