Is it possible to modify the color of a division row using an onchange event in JQuery?

I am facing a requirement to dynamically change the color of rows based on the dropdown onchange value. The rows are structured in divisions, which were previously tables. There are two main divisions with rows that need to be updated based on dropdown selection.

Specifically, when the value in the dropdown within the first division is changed, I want to update the color of all rows in that division. Additionally, I also need to update the color of rows in the second main division. While this could easily be achieved with tables, working with divisions has proven to be challenging and I am uncertain about its feasibility. Below, you will find my code structure along with the jQuery implementation used for changing colors.

$('#bodyLeft tr').bind('click', function(e) {
  var bodyLeftTable = $("#bodyLeft tr").index(this);
  var vehicleClassVal = $("#bodyLeft tr:nth-child(" + bodyLeftTable + ")").find('.vehicleClass').val();
  if (scheduleStatusVal == 'Volvo') {
    $("#bodyLeft tr:nth-child(" + bodyLeftTable + ")").addClass("runningVehicleClass");
    $("#bodyRight tr:nth-child(" + bodyLeftTable + ")").addClass("runningVehicleClass");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--First Division-->
<div id="bodyLeft">
  <div class="divTableRow">
    <div class="divTableCell divWidth">
      <select class="vehicleClass">
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
                </select>
    </div>
    <div class="divTableCell divWidth">
      <input type="text" name="address">
    </div>
    <div class="divTableCell divWidth" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
      <input type="text" name="pass">
    </div>
  </div>
  <div class="divTableRow">
    <div class="divTableCell divWidth">
      <select class="vehicleClass">
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
                </select>
    </div>
    <div class="divTableCell divWidth">
      <input type="text" name="address">
    </div>
    <div class="divTableCell divWidth" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
      <input type="text" name="pass">
    </div>
  </div>
</div>
<!--Second Division -->
<div id="bodyRight">
  <div class="divTableRow">
    <div class="divTableCell width20">
      <input type="text" name="parts">
    </div>
    <div class="divTableCell width85">
      <input type="text" name="ischecked">
    </div>
    <div class="divTableCell width85" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
      <input type="text" name="validity">
    </div>
  </div>
  <div class="divTableRow">
    <div class="divTableCell divWidth">
      <input type="text" name="parts">
    </div>
    <div class="divTableCell divWidth">
      <input type="text" name="ischecked">
    </div>
    <div class="divTableCell divWidth" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
      <input type="text" name="validity">
    </div>
  </div>
</div>

Answer №1

What is the target of #bodyLeft tr in this context?

I don't see a tr element anywhere in that selector.

It seems like you should be attaching a click event to #bodyLeft.vehicleClass, correct? Make sure to review each selector in your JavaScript code and ensure it aligns with the structure of the HTML.

Alternatively, provide a snippet of relevant table code where the selector should apply.

UPDATE: Upon re-reading the text, I noticed that you used to reference a table. It's clear that you need to update your query selectors. This includes both the JavaScript code you shared and any other related code. Don't forget to also double-check the CSS while you're making adjustments.

Answer №2

Here is a potential solution based on your methodology:

$('#bodyLeft .divTableRow').on('click', function(e) {

  var curRow, curSelect, nextRow, nextSelect;
  
  curRow = $( this );
  
  curSelect = curRow.find('select.vehicleClass');
  
  nextRow = curRow.next();
  
  nextSelect = nextRow.find('select.vehicleClass');
 
// These are just examples
  // Please modify with your own code
  curRow.css('background-color', 'yellow');
  curSelect.css('background-color', 'yellow');
  nextRow.css('background-color', 'cyan');  
  nextSelect.css('background-color', 'cyan');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="bodyLeft">
  <div class="divTableRow">
    <div class="divTableCell divWidth">
      <select class="vehicleClass">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="mercedes">Mercedes</option>
        <option value="audi">Audi</option>
      </select>
    </div>
    <div class="divTableCell divWidth">
      <input type="text" name="address">
    </div>
    <div class="divTableCell divWidth" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
      <input type="text" name="pass">
    </div>
  </div>
  <div class="divTableRow">
    <div class="divTableCell divWidth">
      <select class="vehicleClass">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="mercedes">Mercedes</option>
        <option value="audi">Audi</option>
      </select>
    </div>
    <div class="divTableCell divWidth">
      <input type="text" name="address">
    </div>
    <div class="divTableCell divWidth" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
      <input type="text" name="pass">
    </div>
  </div>
</div>
<!--Second Division -->
<div id="bodyRight">
  <div class="divTableRow">
    <div class="divTableCell width20">
      <input type="text" name="parts">
    </div>
    <div class="divTableCell width85">
      <input type="text" name="ischecked">
    </div>
    <div class="divTableCell width85" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
      <input type="text" name="validity">
    </div>
  </div>
  <div class="divTableRow">
    <div class="divTableCell divWidth">
      <input type="text" name="parts">
    </div>
    <div class="divTableCell divWidth">
      <input type="text" name="ischecked">
    </div>
    <div class="divTableCell divWidth" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
      <input type="text" name="validity">
    </div>
  </div>
</div>

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

creating a list of checkboxes with v-for in Vue.js

Having a bit of trouble with CheckBox selection. I am looping through a DataTable and adding CheckBox to it, storing them as an Array. My goal is to have the functionality where selecting the left checkbox removes the right one, and vice versa for the ri ...

What is the best method to compare dates in JavaScript/JQuery to determine if one comes before the other?

I am completely new to JavaScript development and I need to accomplish the task below: I have 2 input tags containing 2 strings representing dates in the format 01/12/2014 (DAY/MONTH/YEAR). These input tags are used to search for objects with a date field ...

Struggle with incorporating a file

As part of the login process, I have two options available: easy login and standard login. The easy login requires an employee ID, birthdate, and captcha answer, while the standard login asks for first name, last name, birthdate, and captcha. To facilitate ...

Arrange four divisions so that they are displayed in pairs on each row when the viewport width is smaller

I have a row of 4 div elements aligned horizontally from left to right. Each div takes up 25% of the screen width. I am looking for a solution to make them wrap when the user resizes the screen instead of overlapping each other. .MenuSett { margin-to ...

Why do I keep encountering a null window object issue while using my iPhone?

Hey there! I've got a React game and whenever the user loses, a new window pops up. const lossWindow = window.open( "", "", "width=500, height=300, top=200, left = 200" ); lossWindow.document.write( & ...

Eliminate unnecessary words from the sentence

I wrote a sentence but it got split at every space. The data is displayed like this: const escapeRE = new RegExp(/([/\?""])/g); const myDatas = data.map(des => des.Sentence.toLowerCase().replace(escapeRE, '').split(' ')); [ [ ...

Refreshing a specific area on a webpage through the use of Ajax technology

I need to update a specific part of my webpage when a user clicks on the 'clear' button. I currently have some code that I borrowed from another answer I found on this site: $('.clear').click(function () { $.ajax({ url: "", ...

Issues with Javascript positioning in Chrome and Safari are causing some functionality to malfunction

My Javascript script is designed to keep an image centered in the window even when the window is smaller than the image. It achieves this by adjusting the left offset of the image so that its center aligns with the center of the screen. If the window is la ...

Managing two simultaneous web service calls in Angular 2

Dealing with two parallel web service calls can be tricky. Sometimes the first call goes through first, and other times it's the second one. The problem arises when the function in my second service requires data from the first service call. I attemp ...

Having trouble with functions in jQuery Ajax calls to ASMX files

I have a form in my web application that submits data through an ajax call to an asmx web service. The insertion of data into the database is successful, however, neither the success nor error function seems to be triggered afterwards. The first alert mess ...

Having trouble implementing a nested grid system in Bootstrap

Struggling with creating a grid layout that looks like the one in this image. I initially thought of setting up a row with 2 divided columns, then adding another row+column within each of those 2 columns to place a card inside. This is an excerpt of my c ...

"Send the response in ExpressJS before making a request to loop through the

I am currently working with a postgres database that contains records with a column format in JSON, which refers to other similar records. The challenge I am facing is retrieving linked records through asynchronous methods due to the nested structure and ...

Having trouble with loading JavaScript during ng build --prod process

The JavaScript file I'm using for multiple emails (multiple_emails.js plugin) works well with ng serve. Here is my code: (function( $ ){ $.fn.multiple_emails = function(options) { // Default options var defaults = { ...

Tips for explaining the structure of a basic Just functor in TypeScript

I am embarking on my first attempt to create a simple interface in TypeScript, and I find myself questioning every step along the way. The core question that troubles me is: How can I best describe this straightforward Jest matcher extension? /** * @par ...

Display user input within a modal dialogue box

I have a subscription form that requires users to enter their name and email address. After clicking on "Compete Now," a pop-up appears asking for workshop information and postal code. The form is functioning correctly as intended. However, I want the em ...

"Troubleshooting the issue of Angular JS ng-click HTML being assigned via InnerHTML but not properly invoking

I am currently working on an AngularJS phonegap application. The HTML in this application consists of a blank table that is dynamically populated using JS Ajax. The Ajax request retrieves the necessary data and fills the table using innerHTML. Each button ...

Determine the number of distinct property values within a JSON API response

Running on a Ruby on Rails backend, I have a JSON API that serves an array of objects with the following structure: { "title_slug": "16-gaijin-games-bittrip-beat-linux-tar-gz", "platform": "Linux", "format": ".tar.gz", "title": "BIT.TRIP BEAT", ...

Accordion Tuning - The Pro and Cons

Here is a functional accordion that I've implemented, you can view it here This is the JavaScript code I am using: $(document).ready(function ($) { $('#accordion').find('.accordion-toggle').click(function () { //Expa ...

Utilize React Material UI Slider to dynamically adjust Border Radius in your web design

Utilizing React Material UI's components like the slider and button is essential for this project. The main objective is to dynamically change the border radius of the button using the value obtained from the slider. However, there seems to be a chall ...

Introducing unnecessary DOM elements when displaying flash messages

When a user saves in my Rails application, it triggers an Ajax request to save the post and then runs the update.js.erb file. This file contains some jQuery code: $('body').append('<div class="message">Saved</div>'); Due t ...