I have a form with table structure html like below:
<tr>
<td>Quotation Category<span class="required" style="color: red">*</td>
<td>
<div class="form-group col-md-9">
<select name="quotation_category" class="form-control" style="width: 80%;" id="quotation_category">
<option value="">Pilih Kategori</option>
<option value="konvensional">Konvensional</option>
<option value="syariah">Syariah</option>
</select>
</div>
</td>
<td class="ujroh-style">Ujroh<span class="required" style="color: red">*</span>
</td>
<td class="ujroh-style">
<div class="form-group col-md-9">
<input type="number" class="form-control" id="ujroh" name="ujroh">
</div>
</td> </tr>
Below is my js:
document.querySelector('#quotation_category').addEventListener('change', () => {
const select_value = document.querySelector('#quotation_category').value
if (select_value === "konvensional") {
document.getElementsByClassName('.ujroh-style').style.display = "none"
} else if (select_value === "syariah") {
document.getElementsByClassName('.ujroh-style').style.display = "revert"
}
})
I want, if I choose 'konvensional', that that selected option Ujroh will appear. If I choose 'syariah', then the selected option Ujroh dissapears. How to do that?
INFORMATION
My code gives this error:
Uncaught TypeError: Cannot set properties of undefined (setting 'display')
at HTMLSelectElement.<anonymous>
This piece of line is causing error
document.getElementsByClassName('.ujroh-style').style.display = "some-value"
You don't need to use . in front because you are already selecting by className
You are using getElements note the s it's plural if that's your intention
you need to loop through all the elements and change the display property on the element not on the array
const elems = Array.from(document.getElementsByClassName('ujroh-style'))
elems.forEach(el => el.style.display = 'your-value')
Three problems:
style property from a collection of elements, and a collection of elements does not have the style property. Only a single element has a style property. Thankfully, you can iterate through each element. on it, which will get you an empty collection. Remove the .revert is not a valid CSS display property enum. Use something elseSo your previous JavaScript code would now be:
document.querySelector('#quotation_category').addEventListener('change', () => {
const select_value = document.querySelector('#quotation_category').value
if (select_value === "konvensional") {
for (const el of document.getElementsByClassName('ujroh-style')) {
el.style.display = "none"
}
} else if (select_value === "syariah") {
for (const el of document.getElementsByClassName('ujroh-style')) {
el.style.display = "inline-block"
}
}
})
One problem is that you use '.classname' as selector in the getElementsByClassName selector. And second you are trying to add a style to a collection of elements, that is not possible. To fix this, you have to add the display style to every element in the collection.
document.querySelector('#quotation_category').addEventListener('change', () => {
const select_value = document.querySelector('#quotation_category').value
if (select_value === "konvensional") {
document.querySelectorAll('.ujroh-style').forEach(x => x.style.display = 'none');
} else if (select_value === "syariah") {
document.querySelectorAll('.ujroh-style').forEach(x => x.style.display = 'inline-block');
}
})
<table>
<tr>
<td>Quotation Category<span class="required" style="color: red">*</span></td>
<td>
<div class="form-group col-md-9">
<select name="quotation_category" class="form-control" style="width: 80%;" id="quotation_category">
<option value="">Pilih Kategori</option>
<option value="konvensional">Konvensional</option>
<option value="syariah">Syariah</option>
</select>
</div>
</td>
<td class="ujroh-style">Ujroh<span class="required" style="color: red">*</span>
</td>
<td class="ujroh-style">
<div class="form-group col-md-9">
<input type="number" class="form-control" id="ujroh" name="ujroh">
</div>
</td>
</tr>
</table>