1

I have multiple buttons I would like to increment a value in different corresponding inputs. I am able to get all the buttons working but they only effect the first input. I realize I can write out three different functions for each button but I know there is a way to utilize one function. Thank you for any help in advance!

Here is code for reference:

let submit = document.querySelectorAll(".submit");
let num = document.querySelector(".num");

submit.forEach((btn)=>{
  btn.addEventListener("click", increment);
});

let value = 0;

function increment(){
  value +=1;
  num.value = value;
}
<div class="container">

  <div class="main">

    <button class="submit">Submit</button>
    <div class="withinDiv"><input type="number" class="num" value="0"></input>     </div>

    <button class="submit">Submit</button>
    <div class="withinDiv"><input type="number" class="num" value="0"></input>     </div>

    <button class="submit">Submit</button>
    <div class="withinDiv"><input type="number" class="num" value="0"></input>     </div>

  </div>

</div>

3
  • You should have a event variable coming in your increment() function. This event has a target field. Try to set an ID per button and identify the target with this ID. Commented Dec 14, 2020 at 18:07
  • 2
    select the next sibling Commented Dec 14, 2020 at 18:09
  • 1
    And <input/> tags are self-closing, <input></input> is not valid HTML Commented Dec 14, 2020 at 18:10

2 Answers 2

2

Select the next sibling and select the input.

const submit = document.querySelectorAll(".submit");

submit.forEach((btn) => {
  btn.addEventListener("click", increment);
});

function increment() {
  const inp = this.nextElementSibling.querySelector("input");
  inp.value = Number(inp.value) + 1;
}
<div class="container">

  <div class="main">

    <button type="button" class="submit">Submit</button>
    <div class="withinDiv"><input type="number" class="num" value="0"> </div>

    <button type="button" class="submit">Submit</button>
    <div class="withinDiv"><input type="number" class="num" value="0"> </div>


    <button type="button" class="submit">Submit</button>
    <div class="withinDiv"><input type="number" class="num" value="0"> </div>

  </div>

</div>

Sign up to request clarification or add additional context in comments.

1 Comment

This worked, I need to familiarize myself with children and siblings and this. Thanks again!
2

That's how i would do it - basically in your "submit" buttons you create attribute (in this case "for" (see EDIT)) that will point to id of input to be incremented. This gives you full flexibility, no matter what html you have.

EDIT

Better use custom attribute, for example data-for, button doesn't have for attribute, as pointed in the comments below

let submit = document.querySelectorAll(".submit");


submit.forEach((btn)=>{
  btn.addEventListener("click", increment);
});



function increment(ev){
  let targetInputId = ev.target.getAttribute("data-for");
  let targetInput = document.getElementById(targetInputId);
  
  targetInput.value = parseInt(targetInput.value) + 1;
}
<div class="container">

  <div class="main">

    <button data-for="num1" class="submit">Submit</button>
    <div class="withinDiv"><input id="num1" type="number" class="num" value="0"></input>     </div>

    <button data-for="num2" class="submit">Submit</button>
    <div class="withinDiv"><input id="num2" type="number" class="num" value="0"></input>     </div>

    <button data-for="num3" class="submit">Submit</button>
    <div class="withinDiv"><input id="num3" type="number" class="num" value="0"></input>     </div>

  </div>

</div>

6 Comments

Buttons don't have a for attribute, but you can use a custom data-foobar (replace foobar with a more meaningful name)
They don't have, yet you still can use it, and yes, i would do "data-for" actually, just, you know, typed right from them head - anyway "just for example"
Just because you can, does not mean you should write or advise people to write invalid HTML
Ok, let me fix that
I see I was beat to it :) I do believe this is a better solution as it does not break if more siblings are added to the HTML.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.