javascript - enable button if user fill the input box, if user emptied or not fill then disable -


i want enable button once user fill or type on input box , disabled if user not fill or emptied input box. sadly, snippet below not working. help, suggestion, recommendation, ideas , clues appreciated.

$(document).ready(function(){    $("input").keypress(function(){      if($(this).val() !== ""){        //user fill or did type on input search box, lets enable button        $("button").attr("disabled", false);      }else if($(this).val() === ""){        //user not fill or emptied box, lets disabled button        $("button").attr("disabled", true);      }    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="text" name="search_input" value="" placeholder="type search here" />  <button disabled>go</button>

try utilizing input , focus events , input being focused while input .length not greater 0 .

note, not expected result if input value multiple space characters ?

$(document).ready(function(){    $("input").on("input focus", function(){      if ($(this).val().length > 0) {        //user fill or did type on input search box, lets enable button        $("button").prop("disabled", false);      } else {        //user not fill or emptied box, lets disabled button        $("button").prop("disabled", true);        }          });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="text" name="search_input" value="" placeholder="type search here" />  <button disabled>go</button>


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -