Denis Matafonov Denis Matafonov - 3 months ago 13
jQuery Question

Select and clear all elements except this

I have 5 inputs with attr

search
.

When user type something in any input, I search requested value in database.
But before searching, I want to clear all other search fields except one being edited.

Here is what I have:

$(document).on('input', 'input[search]', function(){

var searchValue = $(this).val();

//Now I want to clear all other input[search] fields, except $(this)

});


Should be simple, but can't figure out.

Answer

Try this :

$(document).ready(function(){

    $("input[type=search]").on("input",function(){

        $("input[type=search]").not(this).val('');

    })

})

Final code :

<html>
    <title>This is test</title>
    
    <head>
    </head>
    <body>
        
        Search 1 : <input type="search">
        <br><br>
        Search 2 : <input type="search">
        <br><br>
        Search 3 : <input type="search">
        <br><br>
        Search 4 : <input type="search">
        <br><br>
        Search 5 : <input type="search">
        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
            
        $(document).ready(function(){

            $("input[type=search]").on("input",function(){

                $("input[type=search]").not(this).val('');

            })

        })
        </script>
    </body>
</html>