Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Two divs having same class name and ID but at different component of the page. If I try to clear one input the other div input is also cleared

<!-- 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 class="clear-icon">X</button>
    </div>
    <div id="input-outer">
        <input type="text">
        <button class="clear-icon">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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 class="clear-icon">X</button>
    </div>
    <div id="input-outer">
        <input type="text">
        <button class="clear-icon">X</button>
    </div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading