Skip to content Skip to sidebar Skip to footer

Enable And Disable Button

I looked many examples here of enabling and disabling a button in javascript with jquery and any of them worked for me. Here my desperate situation.

Solution 1:

Disabled is a property, not an attribute.

Use:

$('#myButton').prop("disabled", "disabled");

Solution 2:

Javascript:

<script language="javascript" type="text/javascript">


function SetButtonStatus(sender, target)
{

if ( sender.value.length >= 12 )
document.getElementById(target).disabled = false;

else

document.getElementById(target).disabled = true;
}



</script>

HTML:

<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this, 'btnButton')"></asp:TextBox>

<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false" />

Solution 3:


Solution 4:

Did you add jquery library in the head tag?

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
 <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

Solution 5:

Here is what I've done before.

//On document load
$(function(){
      //Set button disabled
      $("input[type=submit]").attr("disabled", "disabled");

      //Append a change event listener to you inputs
      $('input').change(function(){
            //Validate your form here, example:
            var validated = true;
            if($('#nome').val().length === 0) validated = false;

            //If form is validated enable form
            if(validated) $("input[type=submit]").removeAttr("disabled");                              
      });

      //Trigger change function once to check if the form is validated on page load
      $('input:first').trigger('change');
})

Post a Comment for "Enable And Disable Button"