Clear input with Jquery with more than one class

Asked

Viewed 263 times

-2

My input is like this:

<input type="text" class="noclear form-control" value="" name="" required>

Jquery:

form.find('input[class!="noclear"]').val('');

If I leave the input, only with (class="noclear") works right! But I need to leave this other class (form-control), because it is the formatting of the input.

By logic, it is only to clean the input, where there is no class (noclear).

2 answers

1


If you want to select all inputs that nay contains the class noclear, just use the jQuery selector :not() or the method jQuery.not().

let $inputs = $('input:not(.noclear)');
// ou
let $inputs = $('input').not('.noclear');

Example:

$('#clear-button').on('click', function() {
    $('input').not('.noclear').val('');
});
.form-control {
    display: block;
    margin: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input class="form-control noclear" value="noclear">
<input class="form-control" value="Input 2">
<input class="form-control" value="Input 3">
<input class="form-control" value="Input 4">

<button id="clear-button">Clear</button>

  • I tested your solution, but you’re still clearing the field :(

  • So your question is missing information. Because the solution works

  • It was the bloody kkkkkk cache. It worked now! kkkkkkk. Thank you very much! Here I have also tested: ( form.find('input').not(".noclear").val('); ) and returned as correct, as its solution. It seems there are several ways to do this, I liked it.

0

makes it so much simpler:

$(document).ready(function(e) {
    $('.meubotaolimpa').click(function(e) {
        $('input[type="text"],input[type="password"]').not('.noclear').each(function() {
                $(this).val('');
        });
    });
});

.meubotaolimpa is the button class ( serves as ex only )

edited

  • The author of the question wants to do the opposite, clear all inputs except for those who have the class noclear.

  • Just a tip, the $.val() already uses the $.each() internally, so you don’t need to use both, just do $('.noclear').val('') which would work the same. D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.