0

I have two divs

<div class="maca">
    <input type="hidden" value="1">

    <div onclick="removeInput(nearest input type hidden)">Remove</div>
</div> 

<div class="maca">
    <input type="hidden" value="1">

    <div onclick="removeInput(nearest input type hidden)">Remove</div>
</div> 

I want to send as a parameter the value of nearest input type hidden of that div that we are clicking on

0

3 Answers 3

1

Im not 100% sure what you mean with nearest but with this you get the input hidden inside the div. By the way you could also put the element.parentNode.querySelector into the onclick but personally i like it more to split HTML and JS.

function removeInput(element){
let value = element.parentNode.querySelector("input[type='hidden']").value;
console.log(value);
}
<div class="maca">
    <input type="hidden" value="1">

    <div onclick="removeInput(this)">Remove</div>
</div> 

<div class="maca">
    <input type="hidden" value="5">

    <div onclick="removeInput(this)">Remove</div>
</div>

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

4 Comments

but personally i like it more to split HTML and JS Then why are you using inline event handlers?
Good point. True that was missleading. I meaned i'd like to split the view from the business logic. I see people doing calculations inside the onclick HTML tag... If I use inline event handlers and I think there are situations where it makes sense. I try to keep them short and do the math inside a function. In widely used frameworks like angular or react you also use inline events.
No, these frameworks don't use inline event listeners - that's a wide-spread misconception. They use declarative event listeners in templates (e.g. JSX) which are then compiled to use the addEventListener API.
Angular uses as far as I'm aware of directives that uses under the hood also AddEventlistener but I don't understand your point. Even though they technically don't use the inline events it acts, looks and feels like them or do I miss something?
1

You can send the event as a parameter to a javascript function and then find the input via the parentNode. This isn't a literal interpretation of your question since faithfully the "nearest" element is rather complex and probably not what you're looking for.

function removeInput(e) {
  const nearestInput = e.target.parentNode.querySelector("input[type='hidden']");
  console.log(nearestInput);
}
<div class="maca">
  <input type="hidden" value="1" />

  <div onclick="javascript:removeInput(event)">Remove</div>
</div>

<div class="maca">
  <input type="hidden" value="1" />

  <div onclick="javascript:removeInput(event)">Remove</div>
</div>

2 Comments

Haha lol i posted the exact same answer cheers mate 😂😂
@ChristianMeyer 🍻
0

You should not use inline event listeners, which are widely considered bad practice.

Instead, use addEventListener to add the click listeners, and find the input (given your markup structure is fix like you've shown) using previousElementSibling:

for (const remover of document.querySelectorAll('.remove-input')) {
  remover.addEventListener('click', () => remover.previousElementSibling.remove(), { once: true });
}
<div class="maca">
  <input type="hidden" value="1" />

  <div class="remove-input">Remove</div>
</div>

<div class="maca">
  <input type="hidden" value="1" />

  <div class="remove-input">Remove</div>
</div>

Comments

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.