Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i put this code on button click event



JavaScript
 <script type="text/javascript" language ="javascript" >
    Check()
    {
        var fours = document.getElementById("txt4s").value * 4
        var sixs = document.getElementById("txt6s").value * 6
        var totalrun = document.getElementById("txtRuns").value
        var totalruns = fours + sixs
        if (totalrun>totalruns)
         {
         return true;
         }
         else
         {
        
         alert("Total runs should be more then " + totalruns);
       
         return false;
         }
    }
</script>
Posted
Updated 20-Jun-12 3:15am
v2
Comments
ssd_coolguy 20-Jun-12 9:08am    
hey let us know which language you are using?? asp,.net?
rockpune 20-Jun-12 9:12am    
yes
rockpune 20-Jun-12 9:12am    
asp.net
rockpune 20-Jun-12 9:13am    
am using asp.net sir
rockpune 20-Jun-12 9:23am    
i try like this but not getting


<asp:Button ID="btnSaveBatting" runat="server" Text="Save"
onclick="btnSaveBatting_Click" OnClientClick ="Check()"/>

try below code

HTML
<input TYPE="submit" value="Save" name="btnSubmit" onClick="Check()">
 
Share this answer
 
Comments
Maciej Los 20-Jun-12 9:17am    
Good answer, my 5!
ssd_coolguy 20-Jun-12 9:18am    
thank you..:)
rockpune 20-Jun-12 9:24am    
sir

i try like this but not getting

<asp:Button ID="btnSaveBatting" runat="server" Text="Save" onclick="btnSaveBatting_Click" OnClientClick ="Check()"/>
write this line in cs file
C#
Btn.Attributes.Add("onclick", "return check();");
 
Share this answer
 
v2
Comments
rockpune 20-Jun-12 9:35am    
still not getting any result
hey my above answer was for asp.

try below code in asp.net

add this code in page load event..

VB
Button1.Attributes.Add("onclick", "return check();")


and the main thing is that... u missed javascript...
see the correct script below

JavaScript
 <script type="text/javascript" language ="javascript" >
    function Check()
    {
        var fours = document.getElementById("txt4s").value * 4
        var sixs = document.getElementById("txt6s").value * 6
        var totalrun = document.getElementById("txtRuns").value
        var totalruns = fours + sixs
        if (totalrun>totalruns)
         {
         return true;
         }
         else
         {
        
         alert("Total runs should be more then " + totalruns);
       
         return false;
         }
    }
</script>
 
Share this answer
 
If you missed the function keyword not only in your question above, but also in your code, the handler will never be called (syntax-wise it's not even a handler).

Also, when assigning the handler like this OnClientClick ="Check()" have you really confirmed that the function isn't called, or just your code doesn't work as expected.

Apart from this, where in your aspx are you declaring the script block? Try putting it between the <head></head> tags.

Are you sure JavaScript is enabled for your browser?
 
Share this answer
 
Dear Friend,

Use HTML Button.
Use this event in the button OnClientClick ="Check()" and dont use runat="server".

Regards,
AP
 
Share this answer
 
try this may be helpful..
C#
<asp:button id="btnCloseRfix" cssclass="groovybutton" runat="server" text="Cancel" xmlns:asp="#unknown">
                        OnClientClick="Javascript:Check()" />
</asp:button>
 
Share this answer
 

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