PNKJ
asked on
javascript function to calculate the fields
I need a javascript function on page load that would fill in textbox_3 and textbox_4 on client side and add $ sign since it is
amount and can be decimals.
In my form the values for textbox_5, textbox_1 and textbox_2 are filled through database.
textbox_3 should be = textbox_5 * textbox_1 for eg:( .10 * $590.00)
textbox_4 = textbox_5 * textbox_2 for eg:(.10 * $400)
<tr>
<td align="left" >
Rate:<br />
<asp:TextBox runat="server" ID="textbox_5" ></asp:TextBox></td>
</tr>
<tr>
<td width="130" align="left" >
Totals
</td>
<td>
<asp:TextBox runat="server" ID="textbox_1"></asp:TextB ox></td>
<td>
<asp:TextBox runat="server" ID="textbox_2"> </asp:TextBox></td>
<td>
</tr>
<tr>
<td>
<asp:TextBox runat="server" ID="textbox_3"></asp:TextB ox></td>
<td>
<asp:TextBox runat="server" ID="textbox_4"></asp:TextB ox></td>
<td>
</tr>
</table>
amount and can be decimals.
In my form the values for textbox_5, textbox_1 and textbox_2 are filled through database.
textbox_3 should be = textbox_5 * textbox_1 for eg:( .10 * $590.00)
textbox_4 = textbox_5 * textbox_2 for eg:(.10 * $400)
<tr>
<td align="left" >
Rate:<br />
<asp:TextBox runat="server" ID="textbox_5" ></asp:TextBox></td>
</tr>
<tr>
<td width="130" align="left" >
Totals
</td>
<td>
<asp:TextBox runat="server" ID="textbox_1"></asp:TextB
<td>
<asp:TextBox runat="server" ID="textbox_2"> </asp:TextBox></td>
<td>
</tr>
<tr>
<td>
<asp:TextBox runat="server" ID="textbox_3"></asp:TextB
<td>
<asp:TextBox runat="server" ID="textbox_4"></asp:TextB
<td>
</tr>
</table>
Is this not the same as this: https://www.experts-exchange.com/questions/23094948/Auto-calculate-and-add-sign-in-javascript.html
ASKER
No this is different one.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
I tried to use this function but it is not working. Looks like the way i am calling this function on my page is not correct
I have a master page and many.aspx pages .
I am trying to add attribute on my codebehind page as
myMasterpage.Attributes.Ad d("onload" , "init();")
Im my master page I have this function already for onload
Public Sub SetBodyOnLoadScript(ByVal strOnLoad As String)
body.Attributes.Add("onloa d", "javascript:" & strOnLoad)
End Sub
If I use myMasterpage.Attributes.Ad d("onload" , "javascript;" "init();") I get error object does not support this method
I tried to use this function but it is not working. Looks like the way i am calling this function on my page is not correct
I have a master page and many.aspx pages .
I am trying to add attribute on my codebehind page as
myMasterpage.Attributes.Ad
Im my master page I have this function already for onload
Public Sub SetBodyOnLoadScript(ByVal strOnLoad As String)
body.Attributes.Add("onloa
End Sub
If I use myMasterpage.Attributes.Ad
Then rename MY init to hielo:
function hielo()...
then at the end of your already existing init, simply call hielo();
Nothing to it!
function hielo()...
then at the end of your already existing init, simply call hielo();
Nothing to it!
ASKER
var t1 = clean(document.getElementB yId("textb ox_1").val ue);
What does clean do?
I have to add this function on windows onload but looks like the syntax is not correct
myMasterpage.Attributes.Ad d("onload" , "init();")
I am sure it will work
What does clean do?
I have to add this function on windows onload but looks like the syntax is not correct
myMasterpage.Attributes.Ad
I am sure it will work
ASKER
body.Attributes.Add("onUnl oad", "init();");
}
In my mster page body id "body'' runat = server
How can I reference the body tag in my code behind file and correctly add the javascript onUnload event, which calls the function "init();??? I am using vb.net found this code in c charp but don't know the syntax for vb.net
{
MasterPageBodyTag = (HtmlGenericControl)Page.M aster.Find Control("M ainBody");
MasterPageBodyTag.Attribut es.Add("On load", "Popup()");
}
}
In my mster page body id "body'' runat = server
How can I reference the body tag in my code behind file and correctly add the javascript onUnload event, which calls the function "init();??? I am using vb.net found this code in c charp but don't know the syntax for vb.net
{
MasterPageBodyTag = (HtmlGenericControl)Page.M
MasterPageBodyTag.Attribut
}
>>What does clean do?
It takes a string and cleans it so that the only thing that remains is either numeric or a period
>>I have to add this function on windows onload but looks like the syntax is not correct
I am not sure what you mean by "this" when you say "this function". To clarify things, YOU already had a function named init AND your page was already calling YOUR init:
Im my master page I have this function already for onload
Public Sub SetBodyOnLoadScript(ByVal strOnLoad As String)
body.Attributes.Add("onloa d", "javascript:" & strOnLoad)
End Sub
Since you cannot have to functions with the same name, rename MY init function to hielo. Then you can modify YOUR init function so that the last statement in it is to call MY former init function now named hielo:
function init()
{
...
//the original statemens for init are above this line
hielo();//added call to hielo
}
I am not 100% sure, but I believe that an alternate way of calling hielo would have been to simple add it to the attributes after init, and that way you don't need to modify YOUR original init:
myMasterpage.Attributes.Ad d("onload" , "init(); hielo();")
>>If I use myMasterpage.Attributes.Ad d("onload" , "javascript;" "init();") I get error
I believe that line should have been:
If I use myMasterpage.Attributes.Ad d("onload" , "javascript: init();")
The way you coded it is as if though you are passing it three arguments AND forgot the comma to separate the second and third arguments.
>>onUnload event, which calls the function "init();???
All along you have been working with the "onload" event handler to call init, which is typically meant to be called as soon as the page loads (init is short for initialize). The "onunload" is triggered as soon as the browser needs to redraw the screen (refresh, go to another page withing the site, leave the site altogether). I doubt you "need" to call the onunload event. If you think you do, kindly explain.
It takes a string and cleans it so that the only thing that remains is either numeric or a period
>>I have to add this function on windows onload but looks like the syntax is not correct
I am not sure what you mean by "this" when you say "this function". To clarify things, YOU already had a function named init AND your page was already calling YOUR init:
Im my master page I have this function already for onload
Public Sub SetBodyOnLoadScript(ByVal strOnLoad As String)
body.Attributes.Add("onloa
End Sub
Since you cannot have to functions with the same name, rename MY init function to hielo. Then you can modify YOUR init function so that the last statement in it is to call MY former init function now named hielo:
function init()
{
...
//the original statemens for init are above this line
hielo();//added call to hielo
}
I am not 100% sure, but I believe that an alternate way of calling hielo would have been to simple add it to the attributes after init, and that way you don't need to modify YOUR original init:
myMasterpage.Attributes.Ad
>>If I use myMasterpage.Attributes.Ad
I believe that line should have been:
If I use myMasterpage.Attributes.Ad
The way you coded it is as if though you are passing it three arguments AND forgot the comma to separate the second and third arguments.
>>onUnload event, which calls the function "init();???
All along you have been working with the "onload" event handler to call init, which is typically meant to be called as soon as the page loads (init is short for initialize). The "onunload" is triggered as soon as the browser needs to redraw the screen (refresh, go to another page withing the site, leave the site altogether). I doubt you "need" to call the onunload event. If you think you do, kindly explain.
ASKER
In my master page(code behind) I have this sub
Public Sub SetBodyOnLoadScript(ByVal strOnLoad As String)
body.Attributes.Add("onloa d", "javascript:" & strOnLoad)
End Sub
function placefocus()
{
alert ("Hello I am here");
var t1= clean(window.document.getE lementBy(' <%=textbox _1.ClientI D %>').value); alert (t1);
}
I tried adding this placefocus function to my page_load event to test
myMasterpage.SetBodyOnLoad Script("pl acefocus() ;")
IT gives me the alert for hello I am here but for the next line it says object does not support this property or method. Also tried to see if I can call on blur to test but it still gives the error object does not support this property or method
Public Sub SetBodyOnLoadScript(ByVal strOnLoad As String)
body.Attributes.Add("onloa
End Sub
function placefocus()
{
alert ("Hello I am here");
var t1= clean(window.document.getE
}
I tried adding this placefocus function to my page_load event to test
myMasterpage.SetBodyOnLoad
IT gives me the alert for hello I am here but for the next line it says object does not support this property or method. Also tried to see if I can call on blur to test but it still gives the error object does not support this property or method
It sounds like you are not sending all the javascript code to the browser. You need to open your page via the browser, view the source code the browser gets, and make sure all the "necessary" code is there. In this case the browser needs the "clean" function AND due to this:
<%=textbox_1.ClientID %>
You must verify the you have a text field with UNIQUE id and that the id is the same as the one being generated by the line above.
<%=textbox_1.ClientID %>
You must verify the you have a text field with UNIQUE id and that the id is the same as the one being generated by the line above.
ASKER
Excellent works fine It was mistake Instead of GetElementById I types GetElementBy