Vishal Nagra
Vishal Nagra

Reputation: 113

On blur event not working gives error

On blur event not working give error which written below.I just simply want to multiply the value in two textboxs.What i am doing wrong.

 TypeError: document.getElementById(...) is null .  
 var txt8 = document.getElementById("txtQuantity").value;   

<script type="text/javascript">
function CalcSellPrice2() {

    var txt8 = document.getElementById("txtQuantity").value;
    var txt9 = document.getElementById("txtUnit").value;
    document.getElementById("TextBox10").value = txt8 + txt9;
    document.getElementById(txtTotal).innerHTML = TotalPriceValue;
    }


</script>

<asp:TextBox ID="txtQuantity" runat="server" CssClass="span6" 
                                          onblur="CalcSellPrice2();"></asp:TextBox>

Upvotes: 2

Views: 131

Answers (2)

Usman
Usman

Reputation: 3278

Since TextBox is asp.net control so you should have to use ClientID in order to acces your asp controls because asp.net rander HTML with markup and concatenating the ID values of each parent naming container with the ID value of the control (Refer to THIS) as

<%= txtQuantity.ClientID %>

So your script is look like this

var txt8 = document.getElementById(" <%= txtQuantity.ClientID %>").value;

OR

The other way to access ASP.NET controls is to use attribute ClientIDMode, once you set it to Static it doesn't concatenate ID values of each parent naming container. so your HTML will look like as follows.

<asp:TextBox ClientIDMode="Static" ID="txtQuantity" runat="server" CssClass="span6" onblur="CalcSellPrice2();"></asp:TextBox>

in this way you can simply access your asp controls as you have used in your question without using ClientID

Upvotes: 3

sangram parmar
sangram parmar

Reputation: 8726

add attribute ClientIDMode="Static" to textbox

<asp:TextBox ClientIDMode="Static" ID="txtQuantity" runat="server" CssClass="span6" 
                                          onblur="CalcSellPrice2();"></asp:TextBox>

Upvotes: 0

Related Questions