Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a textbox with name txt1.

In javascript, I am making txt1 mandatory, but now I have a added dropdownlist.

So, now txt1 is mandatory only when, the specific value of dropdown is selected.

That means, when I select name from dropdown, then txt1 is mandatory and when I select class from dropdown the txt1 should set to visible off.

Now I make txt1 visible to false, when i select class in dropdown, but it is throwing javascript error that txt1 is null in following line.

But when I select name from dropdown then no error comes because it is visible.
JavaScript
if(document.getElementById('<%=Txt1.ClientID%>').value=="")
Posted
v2
Comments
Please post the whole code...
efkah 3-Aug-12 5:17am    
how do you set the visible style? and: whats the problem? you dont need it anyways;-) just write if (document.getElementById('bla') == null) instead!
enhzflep 3-Aug-12 5:21am    
Well, two things I can think of that would make it fail are
1.) variable names are Case Sensitive. i.e txt1 and Txt1 are different vars
2.) If the textbox's name is txt1, you're not going to locate it by searching for an element that has the id of Txt1

1 solution

If you make it not visible in your ASP.NET code, then it won't be rendered. As such, it is indeed null, it is not there. I'm not sure why the other answerers are confused, it's obvious you're using ASP.NET from the code you posted, and that this is how it works.

Overall, you should always write code that checks if an object exists before accessing it. You should move towards using jquery, which would solve your issue ( it just doesn't return anything if the object is not found ), and move to ASP.NET MVC if you can, in the future.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900