Home > database >  Why does setting maxlength for a textarea not work with asp?
Why does setting maxlength for a textarea not work with asp?

Time:01-11

I am adding the attribute to the textbox inline, but when the page loads, that attribute is not there. I do not have this issue with text boxes that do not have the TextMode="MultiLine" property.

My code:

<asp:TextBox runat="server" ID="txtComments" TextMode="MultiLine" CssClass="form-control" maxlength="500"></asp:TextBox>

When I inspect with the browser, this is what is built:

<textarea name="txtComments" rows="2" cols="20" id="txtComments" ></textarea>

What do I need to do to set the maxlength for the textarea?

CodePudding user response:

My solution was to add a bit of javascript to the end of the page. Here is my solution.

document.getElementById("txtComments").setAttribute('maxlength', '500');

The result from inspecting the page showed that it took.

<textarea name="txtComments" rows="2" cols="20" id="txtComments"  maxlength="500"></textarea>

I still don't understand why it doesn't work by setting the attribute inline.

  • Related