Home > database >  Two divs having same class name and ID but at different component of the page. If I try to clear one
Two divs having same class name and ID but at different component of the page. If I try to clear one

Time:01-01

<!-- language: lang-js -->

    $('.clear-icon').click(function(){
      $('input').val('');
     })


<!-- language: lang-html -->

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="input-outer">
        <input type="text">
        <button >X</button>
    </div>
    <div id="input-outer">
        <input type="text">
        <button >X</button>
    </div>

<!-- end snippet -->

I am trying to clear one value alone but both the values are getting deleted. How can I control it by clearing only the input field of which button is clicked

CodePudding user response:

Add siblings

$('.clear-icon').click(function(){
      $(this).siblings('input').val('');
     })

$('.clear-icon').click(function(){
      $(this).siblings('input').val('');
     })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="input-outer">
        <input type="text">
        <button >X</button>
    </div>
    <div id="input-outer">
        <input type="text">
        <button >X</button>
    </div>

  • Related