Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi I just wanna know how can I disable a button in onmouseover event

When I point on a button it will check if a textbox is not empty
if its empty button will be disable/unclickable else button will be enable

JavaScript
function btnCheck(){
if (txt.value.length == 0 )
{
    document.getElementById('<%= Button1.ClientID %>').disabled = true;
}
else
{
    document.getElementById('<%= Button1.ClientID %>').disabled = false;
}
}


ASP.NET
<asp:Button ID="Button1" onmouseover="btnCheck();" runat="server" Text="Submit" />     
Posted
Updated 18-Apr-13 15:15pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Apr-13 21:03pm    
Sorry, not a question.
—SA
iMaker.ph 18-Apr-13 21:14pm    
My question is whats wrong with the code? I can't disable a button, am I doing something wrong?
iMaker.ph 18-Apr-13 23:17pm    
this is my situation
when textbox is empty the button will be disabled but when I input a text on textbox the button still disabled how can I solve this anyidea ?
Rockstar_ 18-Apr-13 23:54pm    
Check my solution, Again onmouseout u have to write code for enabling the button, I suggest u to use JQuery, u can do such silly things easily..

hi Dear,

Check the below code, it is running successfully....

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">

        function btnCheck() {

            var txt = document.getElementById('<%= txt.ClientID %>').value;
            if (txt.length == 0) {
                document.getElementById('<%= Button1.ClientID %>').disabled = true;
            }
            else {
                document.getElementById('<%= Button1.ClientID %>').disabled = false;
            }
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

<asp:TextBox runat="server" ID="txt"></asp:TextBox>
<asp:Button ID="Button1" onmouseover="btnCheck();" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>

HTML

 
Share this answer
 
Comments
iMaker.ph 19-Apr-13 1:30am    
try adding onmouseout on button1
function btnDflt(){
document.getElementById('<%= Button1.ClientID %>').disabled = false;
};
it still on disable... try making alert onmouseover I've notice that on my 1st hover of the button alert will appear but on the second,3rd... alert doesn't appear anymore..
Rockstar_ 19-Apr-13 2:29am    
it is better to use Jquery's Toggle() function...
 
Share this answer
 
The code above will only work once
my understanding is this

at first when mouseover on button it will check if the txtbox is empty or not and if its empty

the button will be disabled and when I mouseover again the button the event won't occur due to the button is disabled

so my solution is to disabled the button on pageload and add onChange event on textbox to check if its empty or not and then enable the button if its not empty

JavaScript
window.onload = function btnDflt(){            
        document.getElementById('<%= Button1.ClientID %>').setAttribute('Disabled','Disabled');
  };

   function chkItem()
    {    
    var txt = document.getElementById('<%= TextBox1.ClientID %>').value;
        if (txt.length != 0  )
        {                        
            document.getElementById('<%= Button1.ClientID %>').removeAttribute('Disabled');
        }
    }


ASP.NET
<asp:button id="Button1" runat="server" text="Check Input"/>     


Feel free to post/comment when you have a better solution cheers :)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900