JCAguilera
JCAguilera

Reputation: 976

Do something when I clear an input

I have a jquery script that whenever I type something in an input, a checkbox is checked.

So, I want to uncheck that checkbox when I delete all data in that input, with backspace, and do other actions too.

Edit:
I have multiple input and checkbox boxes. Some of you asked me for the code so this is it (They are multiple checkboxes and inputs in a table like this, all with different names):

<tr><td class="chk">
<input type="checkbox" name="has_mic" autocomplete="off">
&nbsp;Micr&oacute;fono</td>
<td class="txt"><input type="text" class="nsize upper" name="mic_obs"></td></tr>

And the script:

$("input[type=text]").keydown(
        function(){
            $(this).parent('.txt').siblings('.chk').find('input[type=checkbox]').prop('checked', true);
        });

So, with this code, whenever I type in the input, the checkbox in the same <tr> is checked. What I want to do, is when I delete all that I typed, the checkbox gets unchecked and preform some other functions.

Upvotes: 1

Views: 56

Answers (1)

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

You can do it like following.

$('input[type=text]').on('input', function () {
    if(this.value)
        $('input[type=checkbox]').prop('checked', true)
    else
        $('input[type=checkbox]').prop('checked', false)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
<input type="checkbox"/>

Upvotes: 5

Related Questions