What are some alternative ways to utilize jQuery siblings() without restricting it to the same parent element?

In the code provided below as Non-functional code [1], it will function correctly with siblings() to disable other input fields if their sum or amount exceeds a specified maximum value. This works effectively when all input fields are within the same parent element.

For instance:

<parent>
  <child input>
  <child input>
  <child input>
  <child input>
</parent>

If you follow the CODE ELEMENT STEPPING technique outlined above on the following code, it should work successfully.

Non-functional code [1]:

$('.variations input').on('change input mouseup keyup', function() {

  var maxVal = 15; //Maximum value defined here
  var sum = 0;

  $('.variations input').each(function() {
    sum += +$(this).val();
  });

  if (sum >= maxVal) { //Total cannot exceed maxVal
    $(this).siblings().not(this).prop('disabled', true);
  } else {
    $(this).siblings().not(this).prop('disabled', false);
  }
});

HTML:
<div class="variations">
  <input type="number" name="sRewards" value="" class="inputReward" />
</div>
<div class="variations">
  <input type="number" name="sReward1" value="" class="inputReward" />
</div>
<div class="variations">
  <input type="number" name="sReward2" value="" class="inputReward" />
</div>
<div class="variations">
  <input type="number" name="sReward3" value="" class="inputReward" />
</div>

I am curious about how jQuery siblings() can be utilized for interactions with other child elements.

In real scenarios, I might need to traverse multiple table elements:

https://jsfiddle.net/L01uexv1/1/

Any suggestions regarding my actual code situation would be greatly appreciated. Thank you!

Answer №1

To start, utilize the .parent() function to locate all sibling 'input' elements, then simply disable them by using the .prop() function.

$('.variations input').on('change', function() {

  var maxVal = 15; //Maximum value defined here
  var sum = 0;

  $('.variations input').each(function() {
    sum += +$(this).val();
  });

  if (sum >= maxVal) {
    $(this).parent().siblings().find('input').prop('disabled', true);
  } else {
    $(this).parent().siblings().find('input').prop('disabled', false);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="variations">
  <input type="number" name="sRewards" value="" class="inputReward" />
</div>
<div class="variations">
  <input type="number" name="sReward1" value="" class="inputReward" />
</div>
<div class="variations">
  <input type="number" name="sReward2" value="" class="inputReward" />
</div>
<div class="variations">
  <input type="number" name="sReward3" value="" class="inputReward" />
</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

When swiping right with Swiper.js, the slides are jumping by all, skipping the following slide, but the left swipe functions correctly

Here is the code I used for my swiper element: new Swiper("#swiper-pricing", { slidesPerView: 1.3, spaceBetween: 30, centeredSlides: true, loop: true, keyboard: { enabled: true, }, autoplay: { delay: 50 ...

How can I use JQuery to select an element with a style attribute in Internet Explorer?

When using JQuery, I have set my target to be the following: <li style="margin-left: 15px;">blah<li> I am using this code to achieve that: $(".block-category-navigation li[style='margin-left: 15px;']").addClass('sub-menu' ...

Whenever the [required] tag switches from being true to false, it generates an event

Initially, I have set up an Angular form using the following code snippet buildForm() { this.form = this.formBuilder.group({ value1: this.formBuilder.control(null), value2: this.formBuilder.control(false) }); } The HTML contains a ...

Combining geometries/meshes in Three.js by eliminating shared regions

My goal is to merge two geometries/meshes (red and blue) into a single unified entity. However, when I create a new geometry and use geometry.merge(), I notice that the inner vertices and faces remain (the green area). I want to eliminate this extra infor ...

Place the text below the adaptable div

Could anyone offer guidance on how to properly align text underneath responsive images within divs? The issue I'm facing is that the divs adjust smoothly based on the viewport size, but the text below them doesn't stay centered as intended. Whi ...

Java Applet for capturing specific sections of a webpage to create a screenshot

Does anyone know of a way (applet or something else) to capture a screenshot of the content within a specific div on a webpage? I came across this Java code for capturing a screenshot of the entire screen: import java.awt.AWTException; import java.awt.Re ...

Issue with module.exports entry in Webpack configuration causing errors

I've been working on setting up webpack but I've hit a roadblock due to this error message. It seems like there's an issue with the entry configuration. When I try to add it without specifying a path, as shown in the tutorial, I receive the ...

Maintaining Existing Filters and Incorporating Additional Filters in React/Next.js Data Filtering

I'm currently facing a challenge with filtering data in React/Next.js. The issue I'm encountering is that whenever I set a new filter, the previous filters get removed. For instance, if a user performs a search, it works fine but the tag filters ...

Routes are no longer being qualified by Express once a subdomain is incorporated

We had developed a compact app that was working flawlessly, but then we were tasked with transforming it into something accessible for others in our organization to utilize... and that led to everything breaking down. Our initial setup included a simple ex ...

CSS - my expectations of reduced content were not met

I'm attempting to create a responsive content box that shrinks properly when the browser size is reduced. https://i.sstatic.net/wQD8j.png However, I am facing an issue where the content (yellow) does not shrink and instead overflows horizontally whe ...

What could be causing the CSS issue following the recent webpack update?

I've taken on the task of updating an older VUE project with the latest npm packages. I managed to update everything successfully and get webpack to compile without any errors, but for some unknown reason, the CSS is no longer rendering in the browser ...

Getting rid of the outline on <html> while tabbing in Firefox

An odd outline keeps appearing as I tab through elements on a website, but only in Firefox and not in Chrome, Safari, or Opera. To identify the element causing the focus, I used Firebug console by typing: document.activeElement. It showed: >>> d ...

Ways to slow down page transition on NextJs

I'm currently working on securing my private pages using a HOC withAuth. While the protection is functioning correctly, I am looking to avoid users seeing a loading screen for a split second while the access token is being retrieved from local storage ...

Handling versioning when a property is deprecated in semver

We've implemented semver for maintaining our CSS libraries, ensuring we adhere to the official guidelines for versioning. However, when we render a class obsolete (or for JavaScript - a property or argument), what is the recommended action? The clien ...

Adding an audio event listener in HTML5

I am having trouble maintaining the limited loop functionality in my HTML5 audioplayer when I try to replace it with buttons and add event handlers to them. The repeat function only seems to work if the checkbox is checked and the second option of the se ...

Cease the continuous scrolling of the display element now

I'm having trouble with my progressbar animation when scrolling. It seems to run multiple times instead of just once. I apologize if there's an error in my code. Could someone please assist me? This is the code I am using: <div class="demo- ...

Angular application featuring scrolling buttons

[Apologies for any language errors] I need to create a scrollable view with scroll buttons, similar to the image below: Specifications: If the list overflows, display right/left buttons. Hide the scroll buttons if there is no overflow. Disable the le ...

Manipulate a JavaScript array for optimal utilization in Google Charts by flattening the days nested within the array

Here is an array [ ["04/21/2021", 405, 85, 30] ["04/18/2021", 350, 135, 30] ["04/16/2021", 335, 120, 30] ["04/15/2021", 420, 100, 30] ["04/15/2021", 405, 85, 30] ["04/15/2021", 350, 13 ...

The same HTML/CSS appears with varying appearances

I have encountered an issue where I created two identical pages, with one page calling the other through a link. Surprisingly, my top menubar changes significantly between the two pages, even though the HTML/CSS code is exactly the same. <html> < ...

I encountered an issue with rate-limiter-flexible where I received an error stating that the Lua redis() command arguments need to be either

I have implemented rate limiting using the rate-limiter-flexible library const redis = require('redis'); const { RateLimiterRedis } = require('rate-limiter-flexible'); This is the code snippet I am working with // Create a Redis client ...