views:

41

answers:

3

I am trying to send an asp control (textbox) to a javascript function.

onblur="CalculateLossRatio(this.value,<%=txtLossRatioCurrentYear.ClientID%>)"

Is is the right way to do this.

A: 

In your situation you could do something like the following:

<asp:TextBox ID="TextBox1" onblur="CalculateLossRatio(this.value, 1)" runat="server" />
<asp:TextBox ID="TextBox2" runat="server" Text="7"/>

<script type="text/javascript">

function CalculateLossRatio(arg1, arg2)
{
    if (arg2 == 1)
    {
        var txt = document.getElementById('<%=TextBox2.ClientID%>');
    }
    else if(arg2 == 2)
    {
        // TODO - get other txt...
    }

    alert(arg1 - txt.value);
}

</script>
Leniel Macaferi
I want to send other textbox too. So I would not be able to use (this).
vaibhav
@vaibhav: I think you'd better go with var txtbox = document.getElementById("txtLossRatioCurrentYear"); for the second textbox...
Leniel Macaferi
But I need to generalize this function. I want to send different textbox to javascript function.
vaibhav
A: 

thats is acceptable, what you are actually doing there is using a preprocessor directive

<%=txtLossRatioCurrentYear.ClientID%> will be substitute on runtime by the generated ID of the control, passing this.value will pass the reference of the control but you can still obtain it using the var txtbox = document.getElementById(controlid);

Oscar Cabrero
A: 

You can send the reference to the sender directly to your js-function(its not clear from question if there is another Textbox):

onblur="CalculateLossRatio(this)"

function CalculateLossRatio(txtBox)
{
    if(txtBox != null){
       var text=txtBox.value;
    }
}
Tim Schmelter