1

This code doesn't display the value, I don't know why?

I have server control:

<asp:TextBox ID="txtTest" runat="server" Visible="false" TextMode="MultiLine"
                    Rows="3" Columns="23" CssClass="white-scroll" />

in javascript function:

var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);

I enter text then click on button that call the javascript function, but the alert box doesn't display the entered text.

EDIT: when I initialize the text with Text="some text", it is displayed in alert, I want to enter text in client side and get the value of it in the Javascript function.

Thanks

3
  • What do the alert display? or is there any javascript error? Commented Jul 2, 2011 at 5:03
  • The TextBox is Visible="false" it means it is hidden then where did you entered Text? Commented Jul 2, 2011 at 5:40
  • it set to true in another control, this is a part of other code Commented Jul 2, 2011 at 9:29

5 Answers 5

3

Using label or textbox visible set false so it can access the value in JavaScript

Sol

1)

Make a div and set style="display:none;" so label is not display at UI(browser) but value can access in JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

2

This is because you server Control is called "txtTest" not "txtEventDescription"

change your javascript function to :

var eventText = document.getElementById('<%=txtTest.ClientID%>').value; 
alert (eventText);

EDIT: ok, I see you've now changed the post to show the code and renamed the js control, so above is no longer relevant (for those who are confused by my answer) :-)

The problem is the Visible="false" - this control will not render into the client and will therefore not be accessible via javascript (as the HTML element does not exist client side)

So, hide the element using CSS and then call alert on it. Sample snippet

CSS

.hide-element {
    display: none;
}

HTML Markup

<asp:TextBox ID="txtTest" runat="server" 
        Columns="23" 
        CssClass="white-scroll hide-element"
        Rows="3"
        TextMode="MultiLine"/>

JavaScript

var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);

This way you will definitely get an alert.
You alert is empty because you have not set the property Text for your asp:Textbox

11 Comments

how can you say it is "txtText ?
mm sorry I changed the nae here in the post and forgot to change the other, but that is not the problem
I clicked the edit link and saw the control name was different to the js script...he's now edited his code (I don;t have edit priviledges yet, otherwise would have edited his code to display the <asp:Textbox... that is now showing
+1 dave. @sourabh, that -1 was not fair. You can clearly see that OP has edited the question.
@Dave Long: updated with some sample code. hope you dont mind.
|
0

Make it Visible="true" to your textbox and than test.

<asp:TextBox ID="txtTest" runat="server" TextMode="MultiLine"
                    Rows="3" Columns="23" CssClass="white-scroll" />

2 Comments

this is a part of other code and sure it is visible while testing :)
but it is set to false in your question ??
0

if "txtTest" textbox has visible="false" in that case its not render on html code on client machine and if it hasn't on client's html code then how javascript calls this textbox. Because when javascript search this textbox by its id it doesn't find and it gives an error.

Comments

0

you can assign any other custom attribute to the control i.e.

    <asp:TextBox ID="txtTest" runat="server" Visible="false" TextMode="MultiLine"
                        Rows="3" Columns="23" CssClass="white-scroll"
 clientID="myClientID" />

and then access control using jquery like

var txtVal = $('textbox[clientID=myClientID]').val();

hope it helps.

3 Comments

when I used JQuery, I get the same result; when set text in server I get it, but if I set it on client side, no
then there might be some other issue, this technique is tested :)
this code is inside user control (.ascx file), is there may be a relation?

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.