tags:

views:

41

answers:

3

So there is a a table and a text box in one of the cells

<td>
    <asp:TextBox ID="tbSomeTextBox" Columns="5" runat="server"> % 
</td>

This textbox gets shown if a certain selection is made in a drop down. The problem is that I would like the "%" character to also be hidden or shown with the textbox.

I have tried putting the whole textbox control inside a DIVand in my JQuery hiding the DIV at the same time I hid the textbox.

<td>
    <div id="divSomeDIV"><asp:TextBox ID="tbSomeTextBox" Columns="5" runat="server"> % </div
</td>

But I get an error in my java script that id="divSomeDIV" doesn't exist in the current context.

$("#<%=divSomeDiv.ClientID%>").hide();

Wrapping that single character in a asp:Label seems like overkill.

Any suggestions?

A: 

create an asp.net label, set the label value to be %, and make them both visible or both not visible at the same time... ?

Spooks
+4  A: 

divSomeDiv is running client-side (i.e. no "runat=server"), so there's no need for

$("#<%=divSomeDiv.ClientID%>").hide();

Just do

$("#divSomeDiv").hide();
Rafael Belliard
Because I was trying to use the server side ID, was that the cause of the "does not exist in the current context" error?
pghtech
@pghtech: Indeed. The server did not find the variable on its side, so it's threw the error. If you look at your generated web page code, <%=tbSomeTextBox.ClientId%>'s real name is something like contentPlaceHolder1_tbSomeTextBox. No need for the <%%> tags on the client.
Rafael Belliard
A: 

If you were getting "does not exist" in your Javascript it's probably because your Javascript is running before the div is created in the dom. In other words, this is incorrect.

<script>$("#divSomeDiv").hide()</script>
<div id="divSomeDiv">...</div>

You can either put the Javascript after the div

<div id="divSomeDiv">...</div>
<script>$("#divSomeDiv").hide()</script>

Or you can make your Javascript run after the DOM has loaded.

<script>
  $(function() {
    $("#divSomeDiv").hide()
  });
</script>
<div id="divSomeDiv">...</div>
Dave Aaron Smith