0

I am having an input text field and a button. When the text field is empty the button should disable. when the field is filled the button should be enable. I tried with the following code but not working properly help me!

$(document).ready(function(){
$("#wmclrsrch").attr("disabled", "disabled");

if ( $('#search_number').is(":empty") ) {
    alert ("verdadeiro");
    $("#wmclrsrch").attr("disabled", true);
}
$('#wmsearch').click(function(){
    $("#wmclrsrch").attr("disabled", false);
    $("#wmclrsrch").attr("enabled", true);
});

});

The HTML is:

<form id="form">
   <label for="search_number">Mobile Number:</label>
   <input id="search_number" type="text" />
   <button id="wmclrsrch">Clear Search</button>
</form>

Thanks in Advance,

3 Answers 3

1

You can listen the keyup event and use prop method.

$(document).ready(function(){ 
    $('#search_number').keyup(function(){
        $("#wmclrsrch").prop("disabled", !this.value.length);    
    }).keyup();
});
Sign up to request clarification or add additional context in comments.

Comments

0

Check input text using keyup event :

$('#search_number').keyup(function(){
    if($(this).val() != '')
    {
        //Eneable your button here
    }
    else
    {
        //Disable your button here
    }
});

Comments

0

You can try the following script:

<script>
/*
 *
 * A tiny jQuery plugin that could control the button's state
 *
 * code from https://github.com/jvyyuie/snbutton
 *
 */
var SNButton = {
    init: function(e) {
        var snnode = "#"+$("#"+e).data("snnode");
        $("#"+e).attr("disabled", ""==$(snnode).val());
        $(snnode).on('input propertychange', function() {
            $("#"+e).attr("disabled", ""==$(snnode).val());
        });
    }
}
</script>

<input id="textinput" />
<button id="btn" data-snnode="textinput">Button</button>

<script>
SNButton.init("btn");
</script>

It is a very simple jquery plugin that disables button when some input filed is empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.