Hide a button until 2 input is filled

Asked

Viewed 613 times

1

I intend to hide each of the 3 buttons until the 2 inputs that are on top of each button are filled , I had already tried and it works , but if I try to add more than once it is always visible even with nothing in the input

    <div id="addElements">
      <input type="text" name="exame" id="examedisciplina"  placeholder="Nome da disciplina">
      <input type="date" id="dataExame" >

      <button type="button" id="add_exame" onclick="addToTableExame()" >Adicionar Exame</button>

       <input type="text" name="teste" id="testedisciplina"  placeholder="Nome da disciplinas">
        <label id='label5'>Data:</label> <input type="date" id="dataTeste" >

       <button type="button" id="add_teste" onclick="addToTableTeste()">Adicionar Teste</button>

       <input type="text" name="trabalho" id="trabalhodisciplina"  placeholder="Nome da disciplinas"><p></p>
        <label id='label6'>Data:</label> <input type="date" id="dataTrabalho" >


      <button type="button" id="add_trabalho" onclick="addToTableTrabalho()">Adicionar Trabalho</button>
    </div>

2 answers

1


You can apply a keyup to the input and go checking their value for example:

$(document).on('keyup', '.form input', function(){
     var val1 = $('.input1').val();
     var val2 = $('.input2').val();
     if(val1 != "" && val2 != ""){
         $('.botao').show();
     }else{
         $('.botao').hide();
     }
});

Or you can keep the button visible and the moment the user presses you check if the fields are empty and if they are you show some message, as:

$('.meuButton').click(function(){
    $(".seuForm input").each(function() {
       if($(this).val() === ""){
          alert("Campos em branco");
       }else{
           //execute sua função aqui
       }
    });
});

Or

$('.meuButton').click(function(){
    var val1 = $('.meuInput1').val();
    var val2 = $('.meuInput2').val();
    if(val1 != "" && val2 != ""){
        //execute sua função aqui
    }else{
        alert("Campos em branco");
    }
});

I haven’t tested the second method, but it should work. Note: If this is a function that will keep repeating itself to each line you add, you should reset the values applied to the button in its function.

  • What the AP needs is to show/hide if each 2 inputs are filled. In your first example only check an input.

  • 1

    My idea was to hide the button and only appear when the 2 inputs are filled

  • @I have changed example 1 so that you can check inputs, and still use the hidden button and show if the two fields are filled here an example http://jsfiddle.net/A7UbK/58/

  • The point is that one of the inputs is of the date type

  • @Forcing only changes the input type to date, if the date is not filled in it will be recognized as a box and the button will not be shown http://jsfiddle.net/A7UbK/59/

  • the button is visible when I write in only 1

  • Are you sure? for here at least in the fiddle it is only visible when the two fields are filled, and when I clear a field the button disappears

Show 2 more comments

0

You can do it like this:

(function(bts) {
    bts.each(function() {
        var inputs = $(this).closest('section').find('input');
        inputs.on('change', verificarInputs.bind(this, inputs.get()));
    });
    function verificarInputs(inputs) {
        var preenchidos = inputs.filter(i => !i.value).length == 0;
        $(this).toggle(preenchidos);
    }
})($('#addElements button'));
#addElements button {
	display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="addElements">
    <section>
        <input type="text" name="exame" id="examedisciplina" placeholder="Nome da disciplina">
        <input type="date" id="dataExame">
        <button type="button" id="add_exame" onclick="addToTableExame()">Adicionar Exame</button>
    </section>
    <section>
        <input type="text" name="teste" id="testedisciplina" placeholder="Nome da disciplinas">
        <label id='label5'>Data:</label>
        <input type="date" id="dataTeste">
        <button type="button" id="add_teste" onclick="addToTableTeste()">Adicionar Teste</button>
    </section>
    <section>
        <input type="text" name="trabalho" id="trabalhodisciplina" placeholder="Nome da disciplinas">
        <label id='label6'>Data:</label>
        <input type="date" id="dataTrabalho">
        <button type="button" id="add_trabalho" onclick="addToTableTrabalho()">Adicionar Trabalho</button>
    </section>
</div>

I organized the HTML a little bit. The idea in Javascript / jQuery is to run a function each time the input changes and generate a boleano to know if everything is filled:

var preenchidos = inputs.filter(i => !i.value).length == 0;

then show or hide the button according to the value of preenchidos with

$(this).toggle(preenchidos);

If you want to keep the same HTML you can do so:

(jsFiddle)

(function(bts) {
    bts.each(function() {
        var inputs = $(this).prevAll('input').slice(0, 2);
        inputs.on('change', verificarInputs.bind(this, inputs.get()));
    });

    function verificarInputs(inputs) {
        var preenchidos = inputs.filter(i => !i.value).length == 0;
        $(this).toggle(preenchidos);
    }
})($('#addElements button'));
  • It is not possible to do this without changing the html ?

  • @Forcing yes, it is possible. I added it to the answer. It would look like this: https://jsfiddle.net/zavsLz1m/

  • I don’t know why but in mine the buttons don’t appear even if you have everything written , and copied examine the JSFIDDLE code

  • @Force where are you placing Javasscript? Place at the bottom of the page...

  • Inside the function that occurs when you press the button but now I realized that it doesn’t make sense , where I should put ?

  • @Forcchape puts as I have in the answer, at the end of HTML within tags <script></script>

Show 1 more comment

Browser other questions tagged

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