SA.
SA.

Reputation: 752

javascript not storing value in textbox in asp

I am using javascript in an asp.net page where I am also using html textarea to get the text from users.

I want to store that text in the asp:TextBox below and set the visibility of that textbox to "false".. The problem arises as I use the hidden textbox to store the value , my javascript is not working, and as I set the visibility to "true" it starts working again. but i didn't want to show textbox.. I included the textbox as:

 <asp:TextBox ID="txtboxhead" runat="server" Visible="false"></asp:TextBox>

and i use javascript as:

document.getElementById('txtareahead').readOnly = true;
text = document.getElementById('txtareahead').value;
document.getElementById('<%= txtboxhead.ClientID %>').value = text;

how this problem can be solved.. please help me out..

Upvotes: 2

Views: 1498

Answers (2)

Darren Reid
Darren Reid

Reputation: 2322

The reason your javascript can not access the textbox when the visibility is set to false is because it simply doesn't exist.

This is because the server is processing the request and because it is set to false it doesn't render it to the page. What you want to do is to change the style of the textbox so it is hidden.

Eg below,

<div style="display:none">
<asp:TextBox ID="txtboxhead" runat="server"></asp:TextBox>
</div>

This way your script will still run and the users will not be able to see the textbox.

HTH

Upvotes: 2

Kyle Sletten
Kyle Sletten

Reputation: 5413

Sounds like what you really want is an <asp:HiddenField> it won't display on the page, but you'll be able to change its contents using javascript.

Upvotes: 0

Related Questions