Issues with colored dropdown menu functionality on HTML page

In my code, I want to change the border color of the dropdown based on certain conditions. If the checkbox is checked and the value is 0, I want the border to be highlighted in red. If the value is not zero and the checkbox is checked, I want it to be green. If the checkbox is not selected, I want the border to be gray.

However, I keep getting an error and I can't figure out why.

I attempted to add another 'else' statement in the JavaScript script outside of the initial 'if', but that caused the code to stop working. When I don't include the additional 'else' statement, the code works fine initially, but the red/green border does not go away when I uncheck the checkbox.

setInterval(() => {
              document.querySelectorAll('input[type="checkbox"]').forEach((checkbox, index) => {
                const select = document.querySelector(`#punteggio${index+1}`);
                if (checkbox.checked) {
                  if (select.value === '0') {
                    select.style.borderColor = 'red';
                    select.style.borderWidth = '2px';
                  } 
                }
              });
            }, 100);
[id^="punteggio"] {padding: 8px 10px; border-radius: 5px; border: 1px solid gray; font-size: 16px;}
                section {margin: 30px 0; display: flex; flex-wrap: wrap; justify-content: space-between;}
                
                
                label, input {display:block; margin-bottom: 10px; font-size: 1.2em;}
                label {font-weight: bold; min-width: 150px; display: inline-block;}
    
                input[type="text"], input[type="checkbox"]{padding: 10px;border-radius: 5px; border: 1px solid lightgray; width: 100%;}
            
                button[type="button"] {width: 100%;padding: 10px; background-color: lightblue; border-radius: 5px; color: #000;cursor: pointer;margin-top: 20px;}
                
                #checkbox1 {width: 20px; height: 20px;border-radius: 15px; margin-right: 10px;}
<ol id = "ordered">
                <li>
                  <select id="punteggio1" >
                    <option value="0">0</option>
                    <option value="0.5">0.5</option>
                    <option value="1">1</option>
                    <option value="1.5">1.5</option>
                    <option value="2">2</option>
                    <option value="2.5">2.5</option>
                    <option value="3">3</option>
                    <option value="3.5">3.5</option>
                    ...
                  </select>
                  <input type="checkbox" name="item" value="Secondo esercizio" id = "checkbox1" style="display: inline-block;">Primo esercizio
                </li>
                <hr>
                // More list items with similar structure
              </ol>

Answer №1

setInterval(() => {
    document.querySelectorAll('#ordered li').forEach((li) => {
        const select = li.querySelector('select')
        const checkbox = li.querySelector('input[type=checkbox]')
        if(checkbox.checked) {
            if(select.value == '0') {
                select.style.borderColor = 'red';
            }
            else {
                select.style.borderColor = 'green';
            }
        }
        else {
            select.style.borderColor = 'grey';
        }   
    });
}, 100);

Answer №2

setInterval(() => {
          document.querySelectorAll('input[type="checkbox"]').forEach((checkbox, index) => {
            const select = document.querySelector(`#punteggio${index+1}`);
            if (checkbox.checked) {
              if (select.value === '0') {
                select.style.borderColor = 'red';
                select.style.borderWidth = '2px';
              }else{
                select.style.borderColor = 'green';
                select.style.borderWidth = '2px';
              }
            } 
          });
        }, 100);
[id^="punteggio"] {padding: 8px 10px; border-radius: 5px; border: 2px solid gray; font-size: 16px;}
            section {margin: 30px 0; display: flex; flex-wrap: wrap; justify-content: space-between;}
            
            
            label, input {display:block; margin-bottom: 10px; font-size: 1.2em;}
            label {font-weight: bold; min-width: 150px; display: inline-block;}

            input[type="text"], input[type="checkbox"]{padding: 10px;border-radius: 5px; border: 1px solid lightgray; width: 100%;}
        
            button[type="button"] {width: 100%;padding: 10px; background-color: lightblue; border-radius: 5px; color: #000;cursor: pointer;margin-top: 20px;}
            
            #checkbox1 {width: 20px; height: 20px;border-radius: 15px; margin-right: 10px;}
<ol id = "ordered">
          <li>
            <select id="punteggio1" >
              <option value="0">0</option>
              <option value="0.5">0.5</option>
              <option value="1">1</option>
              <option value="1.5">1.5</option>
              <option value="2">2</option>
              <option value="2.5">2.5</option>
              <option value="3">3</option>
              <option value="3.5">3.5</option>
              <option value="4">4</option>
              <option value="4.5">4.5</option>
              <option value="5">5</option>
              <option value="5.5">5.5</option>
              <option value="6">6</option>
              <option value="6.5">6.5</option>
              <option value="7">7</option>
              <option value="7.5">7.5</option>
              <option value="8">8</option>
              <option value="8.5">8.5</option>
              <option value="9">9</option>
              <option value="9.5">9.5</option>
              <option value="10">10</option>
            </select>
            <input type="checkbox" name="item" value="Secondo esercizio" id = "checkbox1" style="display: inline-block;">Primo esercizio
          </li>
          <hr>
          <li>
            <select id="punteggio2">
              <option value="0">0</option>
              <option value="0.5">0.5</option>
              <option value="1">1</option>
              <option value="1.5">1.5</option>
              <option value="2">2</option>
              <option value="2.5">2.5</option>
              <option value="3">3</option>
              <option value="3.5">3.5</option>
              <option value="4">4</option>
              <option value="4.5">4.5</option>
              <option value="5">5</option>
              <option value="5.5">5.5</option>
              <option value="6">6</option>
              <option value="6.5">6.5</option>
              <option value="7">7</option>
              <option value="7.5">7.5</option>
              <option value="8">8</option>
              <option value="8.5">8.5</option>
              <option value="9">9</option>
              <option value="9.5">9.5</option>
              <option value="10">10</option>
            </select>
            <input type="checkbox" name="item" value="Secondo esercizio" id = "checkbox1" style="display: inline-block;">Secondo esercizio</input>
          </li>
          <hr>
          <li>
            <select id="punteggio3">
              <option value="0">0</option>
              <option value="0.5">0.5</option>
              <option value="1">1</option>
              <option value="1.5">1.5</option>
              <option value="2">2</option>
              <option value="2.5">2.5</option>
              <option value="3">3</option>
              <option value="3.5">3.5</option>
              <option value="4">4</option>
              <option value="4.5">4.5</option>
              <option value="5">5</option>
              <option value="5.5">5.5</option>
              <option value="6">6</option>
              <option value="6.5">6.5</option>
              <option value="7">7</option>
              <option value="7.5">7.5</option>
              <option value="8">8</option>
              <option value="8.5">8.5</option>
              <option value="9">9</option>
              <option value="9.5">9.5</option>
              <option value="10">10</option>
            </select>
            <input type="checkbox" name="item" value="Terzo esercizio" id = "checkbox1" style="display: inline-block;">Terzo Esercizio</input>
          </li>
          <hr>

        </ol>

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

Security clearance: unidentified user upon logging in through passport authentication

I've been attempting to verify a user's authentication, however, when I try to log in the user after successful verification using req.logIn, it doesn't seem to be working properly. router.post('/login', function(req, res, next) { ...

When the button is left alone, its color will change

<!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <bod ...

ASP.NET Core - Populating views with data gradually

I'm currently working on a .Net Core application and I have reached a point in my code where I need to dynamically add data based on user interactions. Initially, I have a table displaying elements, and when a user clicks on any element, I need to ret ...

Converting a Javascript object to an array results in an array with a length of

I am currently working on developing a website for online shopping, where users have the ability to add products from multiple stores to their cart and checkout, similar to the functionality of AliExpress. When users view their cart, the contents are disp ...

Transform an HTML/CSS document into a PDF file

I recently used html/css to create my resume. Is there a method to transform it into a PDF format? Alternatively, do you have any recommendations for using a different tool to write a CV? Any assistance would be greatly appreciated! ...

JS not functioning properly in specific pages causing display issues with page height set to 100%

I am facing an unusual issue where certain pages do not stretch to 100% page height in the middle section, causing the left-hand border to be incomplete. For example, on the 'Brentwood' site (please click on the 'Login' link in the top ...

Retrieve information from HTML form

I am facing an issue with a form that includes an input field of type datetime-local. The form is uploaded using Angular, and I need to convert the local date input to UTC before posting it. Despite trying different methods, I have been unable to resolve t ...

Creating gifs from an array of base64 strings with gifshot doesn't seem to function properly on Firefox browsers

I am currently attempting to utilize the gifshot library from here in order to generate gifs from a canvas. The process involves capturing the canvas using canvas.toDataURL(), then storing these results in an array, which is subsequently passed to the gifs ...

What's the best way to showcase information from a document on the screen?

Every time I visit the '/gallery' route, the data from the 'posts.json' file is retrieved. By utilizing the 'fs' module and the read() method, the data is read from the file and stored in the this.pictures array. However, upon ...

AngularJS Perspectives: Unveiling the Secrets of Successful Implementation

Do you have any tips on troubleshooting AngularJS views? I found a demo at AngularJS: ngView, but the provided jsfiddle doesn't seem to work. I've been trying to play around with it, but still haven't had any success. This is the code I&apo ...

Creating a responsive gallery using CSS techniques

I'm currently working on creating a simple gallery. My goal is to achieve the following design: https://i.sstatic.net/jq8pe.jpg However, the result I'm getting looks like this: https://i.sstatic.net/hSZyj.png The issue I'm facing is that ...

When I expand one panel, I want to automatically close any other opened panel to ensure only one section is expanded at a

I'm experiencing an issue with the Bootstrap 4 accordion. Currently, when I open a collapsed section in the accordion, it remains open even when I click on another collapsed section. What I actually want is for the previously opened section to close ...

Dynamic Pagination with MUI Counting

I'm currently utilizing mui react for my current project. Within the Pagination component, I am required to input the count in order to display the total number of pages. For example, with a total of 27 products, and each page displaying 20 products ...

What is the best way to distinguish between words using commas?

I am trying to separate words with commas so that I can later highlight them on mouse hover. However, I am facing issues with achieving this and I do not want the words to be split with "-" as well. HTML <p class="texthover"> Name, Name-01 ...

JavaScript - Understanding the varying results of an if/else statement when utilizing a previously declared variable

I have a query regarding the code snippet provided below: var d = new Date(); var weekday = ["su", "mo", "tu", "we", "th", "fr", "sa"]; var deliver = weekday[d.getDay()]; if(condition){ if(d.getDay() == 1){ d.setHours(d.getHours() + 24); // ad ...

Check the status of the audio source URL using JavaScript

I am currently live streaming audio to my player using the Soundcloud API. <audio></aidio> <source src="soundcloud-track-url"></source> Within my code, I have added an onerror eventListener for both the <audio> and <sourc ...

The HTML view is unable to display the CSS style due to a MIME-type error

I have recently developed a very simple Express app that is supposed to display a single view called home.html from the view directory. Although the home.html file is being shown, none of the CSS styles I added seem to be loading. The console is throwing t ...

What is the purpose of using .each method with a jQuery selector followed by $(this) in JavaScript?

Can someone explain why the code below uses .each function twice? function searchFunction(value) { //This cycles through each tr and runs the function $('#results tr').each(function() { var found = 'false'; //$(this) then ...

Monitor the input value for any changes in Angular 8 using the listen component

Hey there! I'm currently working with a component that includes the input @Input() userId: number[] = []; to receive a list of user IDs. Specifically, I have integrated this component into another one, such as the news component: <kt-user-post-li ...

Ways to retrieve and upload a blob from a file object

After receiving a file object via an HTML input on-change event in my component.html: onFileSelected(event) { this.selectedFile = event.target.files[0]; } Now, I need to send a POST request to upload the image to the database. The database only acc ...