Leveraging .closest and .find for accessing values for manipulation

Hey there, thanks in advance for your help!

I'm wondering if I'm on the right track by using .closest and .find to extract values from the input field .cab.

The goal is to take those values and then show the appropriate door size in the DIV .cdmyour.

Is there a more efficient way to achieve this? I have around 20 rows to work with, so I'd prefer not to use IDs as it could result in unnecessary code.

$(document).ready(function() {
  $(".cab").keyup(function() {
    var parent = $(this).closest('.cdmrow');
    var width = parent.find(".cdmcolhalf:eq(0)").val();
    var height = parent.find(".cdmcolhalf:eq(1)").val();
    var dwidth = parent.find(".cdmcolhalf:eq(2)").val();
    var dheight = parent.find(".cdmcolhalf:eq(3)").val();
    console.log(width);
    console.log(height);
    console.log(dwidth);
    console.log(dheight);
  })
})
.cdmh1 {
  text-align: center;
}

.cdmrow {
  display: flex;
  flex-direction: row;
  text-align: center;
}

.cdmcol {
  border-style: solid;
  border-width: 1px;
  width: 14.28%;
}

.cdmcol1 {
  width: 14.28%;
}

.cdmcolmeas {
  width: 57.14%;
}

.cdmyourord {
  width: 28.58%;
}

.cdmcolhalf {
  width: 7.142%;
  border-style: solid;
  border-width: .5px;
}

.cdmyour {
  background-color: lightgrey;
}

.cab {
  width: 50%;
  text-align: center;
  margin: 5%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
  <h1 class="cdmh1">
    About Your Space | Cabinet Door Measurements
  </h1>
  <div class="cdmtable">
    <div class="cdmrow">
      <div class="cdmcol1">
      </div>
      <div class="cdmcolmeas">
        Your Cabinet's Measurements
      </div>
      <div class="cdmyourord cdmyour">
        Your Order
      </div>
    </div>
    <div class="cdmrow">
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
      </div>
      <div class="cdmcol cdmyour">
        Single Doors
      </div>
      <div class="cdmcol cdmyour">
        Double Doors
      </div>
    </div>
    <div class="cdmrow">
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
        Opening Width (in)
      </div>
      <div class="cdmcol">
        Opening Height (in)
      </div>
      <div class="cdmcol">
        Reveal Between Double Doors (in)
      </div>
      <div class="cdmcol">
        Desired Overlay (in)
      </div>
      <div class="cdmcolhalf cdmyour">
        Width (in)
      </div>
      <div class="cdmcolhalf cdmyour">
        Height (in)
      </div>
      <div class="cdmcolhalf cdmyour">
        Width (in)
      </div>
      <div class="cdmcolhalf cdmyour">
        Height (in)
      </div>
    </div>
    <div class="cdmrow">
      <div class="cdmcol">
        Door 1
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
    </div>

  </div>
</div>

Answer №1

Almost there! To associate the retrieved values with the corresponding columns, you can use the .text() method.

Concise version :

$(document).ready(function() {
  $(".cab").on('input', function() {
    var parent = $(this).closest('.cdmrow');

    parent.find(".cab").each(function(i, v) {
      parent.find(".cdmcolhalf").eq(i).text($(this).val());
    });
  });
});

Please Note: Remember to update the selector class from cdmcolhalf to cab and it's recommended to utilize the input event for better performance in tracking user inputs.

$(document).ready(function() {
  $(".cab").on('input', function() {
    var parent = $(this).closest('.cdmrow');

    var width = parent.find(".cab:eq(0)").val();
    var height = parent.find(".cab:eq(1)").val();
    var dwidth = parent.find(".cab:eq(2)").val();
    var dheight = parent.find(".cab:eq(3)").val();

    parent.find(".cdmcolhalf:eq(0)").text(width);
    parent.find(".cdmcolhalf:eq(1)").text(height);
    parent.find(".cdmcolhalf:eq(2)").text(dwidth);
    parent.find(".cdmcolhalf:eq(3)").text(dheight);
  })
})
.cdmh1 {
  text-align: center;
}

.cdmrow {
  display: flex;
  flex-direction: row;
  text-align: center;
}

.cdmcol {
  border-style: solid;
  border-width: 1px;
  width: 14.28%;
}

.cdmcol1 {
  width: 14.28%;
}

.cdmcolmeas {
  width: 57.14%;
}

.cdmyourord {
  width: 28.58%;
}

.cdmcolhalf {
  width: 7.142%;
  border-style: solid;
  border-width: .5px;
}

.cdmyour {
  background-color: lightgrey;
}

.cab {
  width: 50%;
  text-align: center;
  margin: 5%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
  <h1 class="cdmh1">
    About Your Space | Cabinet Door Measurements
  </h1>
  <div class="cdmtable">
    <div class="cdmrow">
      <div class="cdmcol1">
      </div>
      <div class="cdmcolmeas">
        Your Cabinet's Measurements
      </div>
      <div class="cdmyourord cdmyour">
        Your Order
      </div>
    </div>
    <div class="cdmrow">
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
      </div>
      <div class="cdmcol cdmyour">
        Single Doors
      </div>
      <div class="cdmcol cdmyour">
        Double Doors
      </div>
    </div>
    <div class="cdmrow">
      <div class="cdmcol">
      </div>
      <div class="cdmcol">
        Opening Width (in)
      </div>
      <div class="cdmcol">
        Opening Height (in)
      </div>
      <div class="cdmcol">
        Reveal Between Double Doors (in)
      </div>
      <div class="cdmcol">
        Desired Overlay (in)
      </div>
      <div class="cdmcolhalf cdmyour">
        Width (in)
      </div>
      <div class="cdmcolhalf cdmyour">
        Height (in)
      </div>
      <div class="cdmcolhalf cdmyour">
        Width (in)
      </div>
      <div class="cdmcolhalf cdmyour">
        Height (in)
      </div>
    </div>
    <div class="cdmrow">
      <div class="cdmcol">
        Door 1
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
    </div>
    <div class="cdmrow">
      <div class="cdmcol">
        Door 2
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcol">
        <input class="cab" type="text">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
      <div class="cdmcolhalf cdmyour">
      </div>
    </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

Is there a way to identify when a <td> element in a table is modified using JavaScript?

Currently, I am working on a Table where the values in the <td> elements change dynamically based on user input from a form. To achieve this, I am utilizing Tangle for creating a reactive document. I am facing a challenge where I need to detect any n ...

Recreate body content with JQuery Mobile

I've been attempting to duplicate the entire HTML content within the tags. However, even though the appearance on screen is correct, none of the links are functioning.</p> <p>To view the fiddle, click here: [Here is a fiddle][1]</p> ...

"execute loop in a strange and peculiar manner using JavaScript

Implement a loop to place markers on the map: for (i = 0; i <= 6; i++) { _coord = prj_markers[i]; alert(i); instance.set_marker(instance, provider, i, _coord, divBlock); } This code displays "0" in an alert once and executes instance.set_m ...

Can you explain the functionality of the NextSibling method?

I have a question about how the property NextSibling behaves when using the method getElementsByClassName(). Let me illustrate the issue with this code example: function Sibling() { var x = document.getElementsByClassName('firstClass')[0]; ...

Troubleshooting: jQuery UI draggable issue on Chrome

Having issues with jQuery draggable feature not working properly on Google Chrome browser Here is the code snippet causing the problem: <script src="js/jquery-1.6.2.min.js" type="text/javascript"></script> <script src="js/jquery-ui-1.8.16. ...

Issues with AJAX functionality not operating properly in a web form, leading to automatic redirection to a PHP script. Possible solutions and troubleshooting

I have a feature on my web application that allows users to add their skills by clicking the "Add a Skill" button. It was working fine until I tried to incorporate something new. The issue is that when I click the "Add a Skill" button, it acts as though it ...

The flow of Node.js persists despite a response being dispatched

The code snippet I have below checks for spaces in a text, verifies a link asynchronously, and handles post requests. However, there is an issue when the email and password fields are null, resulting in an error message "Cannot read property 'trim&apo ...

Durandal attempts to retrieve a view located within the viewmodel directory

Having an issue with durandal's compose binding as it is looking for views under app/viewmodels instead of app/views The ViewModel code: define(["fields/fieldClosures"], function (fields) { var ctor = function(opt) { this.value = opt.valu ...

Receiving inaccurate video duration when the input bar is modified

Initially, I am uncertain whether the issue stems from Javascript or CSS. The code in question involves displaying the corresponding video time when a user hovers over an input bar. The calculation utilized is as follows: ((user mouseX position / input wi ...

Is there a way to intercept and control mousewheel scrolling before it occurs?

I am in search of a way to intercept the event triggered by a mousewheel scroll right before it occurs, in order to prevent the scroll action, execute certain tasks, and then allow the user to regain control. Currently, I have the following code set up to ...

Is it possible to extract style information using Selenium WebDriver in Python?

I am currently attempting to extract the specific typography used by a company on Stylify Me (e.g. for I am interested in retrieving "UberMove, 'Open Sans', 'Helvetica Neue', Helvetica, sans-serif, normal, 52px, 56px, #000000"). Howeve ...

Utilize the onClick event to access a method from a parent component in React

Looking for guidance on accessing a parent component's method in React using a child component? While props can achieve this, I'm exploring the option of triggering it with an onClick event, which seems to be causing issues. Here's a simple ...

Issues with the Bootstrap navbar toggle functionality

Hello there! I hope you're doing well. I've been following the Bootstrap 5 tutorial on Traversy media's YouTube channel, but I'm encountering an issue with the nav toggler. The nav is not showing up for me and I can't figure out wh ...

My goal is to retrieve specific text from an HTML page that contains two distinct classes

I am trying to pull shipping fees from different types of html pages. Each page requires a unique Xpath for extracting the shipping fees. For one type of page, the Xpath is //*[@class="flex flex-row justify-between f6 mid-gray pt2"]//div[2]/text ...

Is it necessary to incorporate express in a Next.js project?

I'm currently working on a website using Next.js. With Next.js, I have access to features like SSR and dynamic routing. Is it necessary for me to incorporate express into my project? If yes, what are the reasons behind needing to use it? What unique ...

"I need assistance creating a communication platform using PHP and JQuery. Can someone provide guidance on how

Table named 'names' is being updated every time a user logs in or out. I am looking for assistance in creating a column that displays the names of users whose status in the 'names' table is set to 'YES'. When a user clicks on ...

Why is the Twitch api map function returning nothing, while the console log is showing output?

Presently, my Nextjs page is making multiple Twitch API calls successfully and displaying the correct data. However, one of the mapping functions is failing to render anything on the page, even though the console log shows the data. Although I am relativel ...

"Trouble arises as Amchart and Ajax encounter data that is not in the

I am trying to utilize AmChart and retrieve data for charts from PHP. Below is my PHP file: $colors = Array("#FF0F00","#FF6600","#FF9E01","#FCD202","#F8FF01","#B0DE09","#04D215","#0D8ECF","#0D52D1","#2A0CD0","#8A0CCF","#CD0D74","#754DEB","#DD ...

Retrieving an array of various responses using Axios

My current code includes a function that retrieves exchange rates for various stocks: export const getRates = (symbole1, symbole2, symbole3) => { const res = [] axios.all([ axios.get(`${baseUrl}/${symbole1}`), axios.get(`${ ...

CSS - Customizing the appearance of consecutive child divs

I'm struggling with adjusting the margin between two child divs when they follow each other. The current margin is set at 3rem, but I want it to be 1rem if both child containers have the class "narrow": Is there a way to achieve this without changing ...