Can a button be designed to exclusively impact the div within which it is located, while buttons in different divs share the same script code?

I have a jQuery script that unchecks checkboxes in form inputs. I'm trying to make it work so it only affects the specific list item it's in, but I've been unsuccessful so far.

$(document).ready(function() {
  $(".check").click(function() {
    $(".myCheck").prop("checked", true);
  });
  $(".uncheck").click(function() {
    $(".myCheck").prop("checked", false);
  });
});
.appslist {
  list-style: none;
  padding: 0%;
  display: block;
  height: 90%;
}

.appslist li {
  display: block;
  width: 18.33%;
  padding-left: 5.356%;
  padding-right: 5.356%;
  margin: 0%;
  float: left;
  max-height: 100%;
}

.ecard {
  height: 100%;
}

.log {
  height: 85%;
}

.logheader {
  height: 15%;
}

.resetbutton {
  float: left;
  height: 100%;
  width: 20%;
}

.logheader {
  text-align: center;
  border-bottom: 2px solid black;
  border: 5px solid black;
  display: flex;
  justify-content: space-between;
  align-content: center;
}

.logheader h1 {
  margin: 0%;
  font-size: 1.5vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<ul class="appslist">
  <li>
    <div class="ecard">
      <div class="logheader">
        <h1>Div1</h1>
        <button type="button" class="uncheck resetbutton"></button>
      </div>
      <div class="log">
        <form>
          <div class="setinput">
            <h6>
            <input type="checkbox" class="myCheck">20</input> Set 1 </h6>
          </div>
        </form>
      </div>
    </div>
  </li>
  <li>
    <div class="ecard">
      <div class="logheader">
        <h1>Div2</h1>
      </div>
      <div class="log">
        <form>
          <div class="setinput">
            <h6><input type="checkbox" class="myCheck"> 20</input> Set 1 </h6>
          </div>
        </form>
      </div>
    </div>
  </li>

Answer â„–1

discover the closest ancestor that matches a specific selector

    $(this).closest('div.ecard').find(".myCheck").prop("checked", true);

$(this).closest('li') could be used as well.

$(document).ready(function(){
    $(".check").click(function(){
        $(this).closest('div.ecard').find(".myCheck").prop("checked", true);
    });
    $(".uncheck").click(function(){
        $(this).closest('div.ecard').find(".myCheck").prop("checked", false);
    });
});
.appslist{ list-style: none; padding: 0%; display: block; height: 90%; }
.appslist li{ display: block; width: 18.33%; padding-left: 5.356%; padding-right: 5.356%; margin: 0%; float: left; max-height: 100%;}
.ecard{height: 100%;}
.log{ height: 85%;}
.logheader{ height: 15%;}
.resetbutton{float: left; height: 100%; width: 20%;}
  .logheader{text-align: center;  border-bottom: 2px solid black; border: 5px solid black; display: flex; justify-content: space-between; align-content: center;}
  .logheader h1{margin: 0%; font-size: 1.5vw;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<ul class="appslist">
          <li><div class="ecard">
            <div class="logheader">
              <h1>Div1</h1>
              <button type="button" class="uncheck resetbutton"></button>
            </div>
            <div class="log">
              <form>
                <div class="setinput"><h6><input type="checkbox" class="myCheck">20</input> Set 1 </h6> </div>
              </form>
            </div>
          </div></li>
          <li><div class="ecard">
            <div class="logheader">
              <h1>Div2</h1>
            </div>
            <div class="log">
              <form>
                <div class="setinput"><h6><input type="checkbox" class="myCheck"> 20</input> Set 1 </h6> </div>
              </form>
            </div>
          </div></li>

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

Removing Click event upon button click - Implementing Vue and Vuetify

In my project using Vuetify, I have implemented a dialog that opens when a button is clicked. The dialog can be closed after completing the required actions. However, in the production environment, the dialog cannot be reopened more than once due to the re ...

At what point does a component detach?

From what I understand, a component in React mounts onto the DOM when it needs to be displayed or when the route requires it. It also renders its child components. So does this mean that a component is unmounted when you navigate to a route that doesn&apos ...

Troubleshooting issue: Click function not responding inside Bootstrap modal

Below is the JavaScript code for my click function $(".addPizza").on("click", function(event) { event.preventDefault(); console.log("hello") let userId = $("#userId").attr("data-id"); let pizzaRecipe = $('#pizza-recipe').val().trim(); ...

Using lodash/underscore to aggregate, sort, and limit data

My data needs to be aggregated, and while MongoDB aggregation would make this easy, I prefer to do it on the client side. Utilizing LoDash or Underscore should help with this task. Let's consider the sample data below: var data = [ { "uI ...

Building nested routes in a protected path using react-router version 6

At the moment, I am utilizing react-router-dom version 6.1.1 and I have a private route in use. Typically, within this private route, I include other routes to maintain my Sidebar. This is how my code appears: // App.tsx const RequireAuth: React.FC<P ...

Determine total number of covid-19 cases with JavaScript

I am looking to incorporate a real-time COVID-19 total cases tracker for Russia on my website. In order to achieve this, I am sending a request to using the following script: function httpGet(theUrl) { var xmlHttp = new XMLHttpRequest(); xmlHttp.o ...

Tips for designing a tilted CSS layout from scratch

I am looking to design a stylish interface using HTML & CSS. Here is the style I want to achieve: Currently, my interface looks like this: Although these are just reference images, you can see that my design does not have the same sleekness as the one in ...

The function .click() fails to execute upon page load, yet it functions successfully when utilized in the console

This is the code I am using: <script> document.getElementById("test").click(); </script> When I try to trigger the click(); function on page load, it doesn't work. However, when I execute it in the console on Firefox, it works ...

Scroll to a specific element on the page while offsetting by a certain

Currently, I have the code below that successfully scrolls down to the ID ('expressRate') when the page loads. However, due to a change in requirements, the page now needs to scroll down to a position 50 pixels above the div with the ID 'exp ...

Use JSON to initiate opening a new tab in JavaScript code

Javascript Code: <script type="text/javascript"> function SaveCustomerInformation() { var data = { SpecialStatusId: AnaOzelDurumId.GetValue(), ContactPersonId: AnaÄ°lgiliPersonelId.GetValue(), }; ...

Conflict arises between entities due to the speed of one

I am working on creating a scenario in my world where a ball enters a tube with multiple sections. To achieve this, I have constructed the tube using a series of box shapes that I manipulate by moving and rotating them to form the complete structure of the ...

What is the method for socket.emit() data that originates from a function?

Using Socket.io with Node.js Sending data from the server to the client is straightforward when using a string or hard-coded variable: var greeting = 'hello'; socket.emit('greeting', greeting); However, things get tricky when trying ...

Text tags do not create new lines; words with different tags appear on the same line without any breaks

Here is my HTML code snippet: <div class="row"> <div class="col-md-6 col-lg-8 col-12 mb-4 order-lg-1 order-2 text-center mt-3"> <div class="rightside w-100 h-100 d-flex justify-content-center align-ite ...

Is it possible to use CSS to create a gap between the cursor and the placeholder text within an input field?

Could you please assist me with a bug I have encountered on an older version of Firefox for OSX (37.0.2)? I have included a screenshot of the issue here: https://i.sstatic.net/dzK0G.png Is there a way to use css to move the first character of the placehol ...

challenge encountered while trying to remove ScrollTop feature on mobile gadgets

Currently facing a challenge with responsive design - I have implemented a sticky navbar effect that works seamlessly on desktop, but causes usability issues on mobile. When scrolling down on the mobile version, the sticky navbar obscures the text on the s ...

divide a lengthy string into several dynamic divisions

I am attempting to divide a lengthy string into multiple dynamic divs once the height of the div reaches a certain limit. Below is the code that I believed would achieve this: $(document).ready(function () { var arr = longishstring.split(' ' ...

What is the best way to make hover text that also copies when you highlight the main text?

Currently, I am utilizing a basic bootstrap tooltip for creating tables with jQuery. However, I require a specific functionality that the standard bootstrap tooltip does not provide. I need the ability to copy the base text and have it appear as "base tex ...

What is the best way to sign up for input so that any modifications are automatically saved to an object?

My HTML form input looks like this: <mat-form-field appearance="fill"> <mat-label>Flowrate: </mat-label> <input id = "flowRate" type="number" matInput> </mat-form-field> In my .ts file ...

Vue.js component fails to load $refs

One of my Vue.js components features a modal view with various embedded references. <template> <div> <b-modal ref="modalDialog" > <b-row> <!-- document --> <b-col> &l ...

The onProgress event of THREE.JS Loader will only be triggered once the loading process has been successfully completed

Despite following numerous tutorials, I am struggling to receive regular updates on the loading progress when loading a model in order to create a loading icon. No matter what method I try, the onProgress function only triggers once, and that's after ...