Ways to display the chosen selection post-selection of the option

My form has a table structure in HTML as shown 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>

This is the javascript I have written:

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 need assistance with making it so that when 'konvensional' is selected, Ujroh option appears. And when 'syariah' is chosen, then the Ujroh option disappears. How can I achieve this?

NOTE
The code above is resulting in the following error:

Uncaught TypeError: Cannot set properties of undefined (setting 'display')
at HTMLSelectElement.<anonymous>

Answer №1

There are a few issues that need to be addressed:

  1. You are attempting to retrieve the `style` property from a group of elements, but only individual elements have a `style` property. To fix this, you can loop through each element.
  2. You are trying to select all elements with a specific class using a string with a period (`.`), which will result in an empty selection. Remove the period.
  3. The `revert` value is not a valid CSS `display` property option. Try using a different value instead.

To correct your previous JavaScript code, it should now look like this:

document.querySelector('#quotation_category').addEventListener('change', () => {
    const selectValue = document.querySelector('#quotation_category').value;
    if (selectValue === "konvensional") {
        for (const el of document.getElementsByClassName('ujroh-style')) {
            el.style.display = "none";
        }
    } else if (selectValue === "syariah") {
        for (const el of document.getElementsByClassName('ujroh-style')) {
            el.style.display = "inline-block";
        }
    }
})

Answer №2

An issue is arising with this particular line

document.getElementsByClassName('.ujroh-style').style.display = "some-value"
  1. Avoid using . at the beginning since you are already targeting by className

  2. You used getElements, note the plural s. If that's your intention, you must iterate through each element and modify the display property of the element, not the array

const elems = Array.from(document.getElementsByClassName('ujroh-style'))
elems.forEach(el => el.style.display = 'your-value')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transform the Angular function into a factory and take advantage of caching

My query differs from others with a similar title. I am attempting to transform a function into a factory in order to share data across controllers, but unlike my other factories, this one is not functioning as expected. I have successfully implemented ot ...

Execute JavaScript code when the selected values match

I have created a script to calculate numbers. However, I want this script to only work when the user selects 'text2' from the dropdown menu (value=5). Unfortunately, the script currently does not seem to be working as expected. When attempting ...

When I request the value of a JavaScript object, I am met with 'undefined'

I have been working on creating a Google map application that involves loading markers from a database and displaying them on a map. To accomplish this, I decided to create an array and define an object as shown below: function shop_info(name, latitude, l ...

Tips for managing Express.js callbacks and modifying an object's property from within a function

I am currently working with two JavaScript files. I have successfully retrieved data from MongoDB using the method bookDao.getActiveBookByCategoryId(). The Issue I Am Facing: Within the categoryDao.js file, I am attempting to update the resultJson.book_c ...

Navigating Maps in a PhoneGap iOS App

Regarding the issue mentioned in this link I am attempting to find a specific location on the native iOS map using the URL below: window.location = "maps:Greensboro+NC" However, it opens the map at my current location instead of the specified location. ...

Exploring the Images directory

I hate to admit it, but I'm really struggling with this question. It's frustrating and I've searched everywhere for an answer. Currently, I'm using WAMP on Localhost with PHPstorm. The issue I'm facing is accessing the images di ...

Centered inline-block divisions

I've been coding for a number of years now, but I've recently started learning CSS. I'm currently facing an issue and need some help. I want to place two divs next to each other and center them. Here's the code I'm using: HTML: & ...

Adjusting the size of images to seamlessly fit within a card in Bootstrap 5 without changing the dimensions of the card

I am facing an issue with a Bootstrap 5 card split into 2 columns. First Column Second column Image1 Text Image2 Text The challenge arises because not all images are the same size, impacting the overall card size. I recently discovered a soluti ...

Looping through required fields in jQuery based on a CSS class

Is there a way to loop through required fields marked with <input class="required">? Currently, only the first input in the loop is being evaluated. How can I traverse the DOM using a loop to check all input boxes? Thank you for your help. While I ...

Creating an interactive dropdown menu using uib-typeahead directive in AngularJS

Managing a data application with large tables can be challenging. We have tried local storage solutions to improve performance on slow connections, but we are encountering difficulties when attempting to implement custom filtering for typeaheads. Even th ...

Ways to stop a JS plugin affecting HTML anchors and preventing them from automatically scrolling to the top

My friend is working on a website and he's using a plugin called Flip Lightbox. The basic functionality of the plugin is that when you click on an image, it flips over and pops out as a lightbox. Everything works fine, however, if you scroll down the ...

jQuery grid view for checking the status as even or odd

I am facing an issue with a gridview where users input integer numbers in a text box, and a jQuery function should display whether the number is even or odd in another text box in the same row. Below is an example with some columns omitted for simplicity: ...

TypeAhead.js and Bloodhound displaying an uneven quantity of search outcomes

I have a frontend setup with TypeAhead and Bloodhound integration, fetching JSON data from a Play/Scala server. The version of Typeahead being used is 0.11.1. Here is how the implementation looks: HTML: <div id="typeahead" class="col-md-8"> < ...

The functionality of Angular UI TimePicker is not functioning as expected

Currently, I am in the process of developing an angular directive that is designed to manage date and time functions. I have integrated Angular's UI Bootstrap into my project but have encountered a peculiar issue with the TimePicker component. While t ...

Retrieve the values from several text fields, save them to a variable, and then transmit them to CodeIgniter

I have multiple input fields with type=text and I need to retrieve their values, store them in a variable, and send them via POST to a CodeIgniter controller. Here is an example code: <input type="text" value="value1" name="in1"> <input type="te ...

Is per-page localStorage the solution?

Can a local storage be created in HTML5 that is only accessible to a single webpage? I am currently exploring the potential of creating self-contained single-page applications and whether users can host them themselves, for example on Dropbox (which offer ...

Ways to modify the focus hue of a react-bootstrap form input file?

I've been using react-bootstrap to style my elements, but I'm having trouble changing the focus color of my input type file. It always shows the default blueish border when focused. Despite trying various approaches like targeting input[type=&quo ...

Why isn't the front-end loading properly upon reloading, and instead displaying JSON data?

Encountering an odd issue post deployment on Heroku today. The setup includes a React front-end and an Express back-end for a social media application. Specifically, there is an issue with the profile page where data is fetched from the back-end route. Eve ...

Attempting to submit a form containing multiple fields that share the same name as an array

I am attempting to submit a form with fields that function as entries in a table For clarity, here is an example: <form method="post"> <table> <tr> <td><label>Name:</label></td> ...

Enhance the appearance of filters in datatables by applying custom classes

I'm currently looking for a way to include an extra custom class in the jQuery datatables filters (Records per page and Search) These elements are displayed as follows: <div id="DataTables_Table_0_length" class="dataTables_length my-custom-class" ...