0

I am trying to fill TextBox on event of two buttons.

<div class="numbers-row">
 <div onclick="var result = document.getElementById('qty'); var qty = result.value; if( !isNaN( qty ) &amp;&amp; qty > 0 ) result.value--;return false;" class="dec qtybutton"><i class="fa fa-minus">&nbsp;</i></div>

 <asp:TextBox ID="qty" runat="server" class="qty" title="qty" maxlength="12" name="qty"></asp:TextBox>

 <div onclick="var result = document.getElementById('qty'); var qty = result.value; if( !isNaN( qty )) result.value++;return false;" class="inc qtybutton"><i class="fa fa-plus">&nbsp;</i></div>
</div>

Image contains two buttons. On Onclick event minus button decremented the value and plus button increment the value, but the value is not reflecting on TextBox. But if i use html textbox like <code><input type="text" class="qty" title="Qty" value="1" maxlength="12" id="qty" name="qty"/></code> it works fine

Image contains two buttons. On Onclick event minus button decremented the value and plus button increment the value, but the value is not reflecting on TextBox. But if i use html textbox like <input type="text" class="qty" title="Qty" value="1" maxlength="12" id="qty" name="qty"/> it works fine

2
  • You know it works OK with an input tag. What's the id attribute of the text box in production? Does it prepend any other info? Commented May 5, 2018 at 1:31
  • No. TextBox does not contains any other info. Commented May 5, 2018 at 1:43

2 Answers 2

1

Asp.Net chnages the ID of the server controls to ensure unique IDs for elements on a webpage. if you are to use the same ID use ClientIDMode="Static" inside your asp:TextBox. or your may get the input reference in javascript by using

document.getElementById('<%=qty.ClientID%>')
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome Mohit... you may accept the answer if it was helpful.
1

If I recall correctly, ASP.NET will add additional identifiers to ensure that the ID is unique. When it's rendered on the client the ID will look more like ctl00$ContentPlaceHolder1$CustomUserControl1$qty. You can read more about this naming convention in the ASP.NET Web Server Control Identification docs.

To solve your problem, you can:

  • use a CSS attribute selector if you know that the id ends with qty, like document.querySelector('[id$="$qty"]')
  • use the exact ID as it is rendered (flakier)
  • change the ASP.NET naming convention

1 Comment

also look into ClientIDMode="Static" on your web form control

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.