Solved

document.getElementById

Posted on 2011-09-18
13
335 Views
Last Modified: 2012-05-12
I am trying to get the reference to an HTML textbox using:
var tb1 = document.getElementById(id);
tb1.style.display = "none";

But, this does not seem to work.
Please let me know what I might be missing. Thanks!
0
Comment
Question by:dshrenik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36557855
The name of the 'id' should be quoted.

var tb1 = document.getElementById("id");
tb1.style.display = "none";

http://www.w3schools.com/jsref/dom_obj_style.asp
0
 

Author Comment

by:dshrenik
ID: 36557859
id is a variable that stores the actual ID
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36557867
wait the dom

window.onload = function() {
    var tb1 = document.getElementById("someid");
    tb1.style.display = "none";
}

Open in new window


if you've asp.net use ClientID : http://beyondrelational.com/blogs/hima/archive/2010/07/16/all-about-client-id-mode-in-asp-net-4.aspx

var tb1 = document.getElementById("<%= Textbox1.ClientID %>");
tb1.style.display = "none";

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

Author Comment

by:dshrenik
ID: 36557868
Using Firebug, this is the source for the HTML element:

<input id="id_1dropdown_0*p_0" class="parameterStyle" type="text" onchange="checkParameter1(this);" name="id_1dropdown_0*p_0" style="border-left: 1px solid grey; border-right: 1px solid grey; border-width: 1px; border-style: solid; border-color: grey;">

Open in new window

0
 

Author Comment

by:dshrenik
ID: 36557872
I can't do this on "window.onload ". I need to do it after the page loads
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36557880
it work : http://jsfiddle.net/FvPK5/
function hide(id) {
    var tb1 = document.getElementById(id);
    tb1.style.display = "none";
}

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36557886
If you're trying to use 'id' as a variable name, I suggest you change it to something else because that has a specific meaning already.  While it is not a reserved word, I would use a variation of it like 'id1' or something that would not be confused with the HTML attribute 'id'.

Are you sure that 'id' contains a valid value?  Your syntax looks ok.  http://www.w3schools.com/jsref/prop_style_display.asp
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36557891
The asterisk is not a legal character in the id. id="id_1dropdown_0*p_0"  From W3C:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36557897
with a dropdown : http://jsfiddle.net/FvPK5/1/
0
 

Author Comment

by:dshrenik
ID: 36557899
It is not a dropdown. It is a textbox (the id is misleading).
0
 

Author Comment

by:dshrenik
ID: 36557944
I am able to retrieve the textbox with this line of code:
var tb1 = document.getElementById(id);

I can display its value by saying:
alert(tb1.value);

However, this is not working:
tb1.style.display = "none";
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36557951
try with an alert after :
tb1.style.display = "none";
alert("do you still see it really before clicking OK?");

Open in new window

0
 

Author Comment

by:dshrenik
ID: 36557978
It was my mistake. It actually works. Thanks!
If possible, please answer this question:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27314630.html
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question