0

I have just tried to add value in Two Text boxes and want to display in third text box.and I also tried to call my JavaScript Function in ASP:TextBox.but the function is not properly. I have Attached my code below to it.

I want to know how to call JavaScript Function in ONCLICK Event on ASP:TEXTBOX

<body>
<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"  ></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
    <asp:TextBox ID="TextBox3" runat="server" onchange="javascript:addition(this)">        </asp:TextBox>
    <br />
</form>
<script>
    var text1 = document.getElementById('<%=TextBox1.ClientID%>');
    var text2 = document.getElementById('<%=TextBox2.ClientID%>');
    var text3 = document.getElementById('<%=TextBox3.ClientID%>');
         var t1 = text1.value;
         var t2 = text2.value;
         var t3;
    function addition(){
t3 = parseInt(tl).value + parseInt(t2).value;
        document.getElementById('TextBox3').value = t3;
    }
 </script>

4
  • Can call JS function in onblur event. Commented Dec 20, 2017 at 9:43
  • Already tried with onblur but it doesn't work for me Commented Dec 20, 2017 at 9:50
  • Yes you are right, see my answer. Commented Dec 20, 2017 at 9:59
  • So the blur was your answer ?!! as you accepted ! as you experienced the blur is not fire when you focusing TextBox3, it is not the thing as you requested,, anyway focus in JQuery or onfocusin in JavaScript is your solution not blur !! Commented Dec 20, 2017 at 10:32

2 Answers 2

1

Try this:

 <asp:TextBox ID="TextBox3" runat="server" onblur ="javascript:addition(this)">  </asp:TextBox> 

if you are using JQuery it is better to use focus event such as:

$('<%=TextBox3.ClientID%>').focus(function() {
  //Additional code here
});

but yes onblur may doesn't work as you want because the blur event is sent to an element when it loses focus as I mentioned, So it is better to use focus event.

Another way is Handeling onfocusin like:

<asp:TextBox ID="TextBox3" runat="server" onfocusin="javascript:addition(this)">  </asp:TextBox> 

the onblur behaves like onfocusout.

Hope will help you.

Sign up to request clarification or add additional context in comments.

Comments

1
$("#TextBox3").blur(function () {
//     Do Somthing
});

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.