473,947 Members | 5,336 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.getEle mentById returning null...

4 New Member
Hey guys...

I am not too knowledged in js and I code in it occasionally, so please excuse the question if it is steight forward...

Now...

Been working on an analogue clock script, customised it exactly to my needs, until I got to the point of calling it in my body...

I am trying to enter the clock inside a div but when I call it by document.getEle mentById nothing happens. Console on firefox says that it returns null. Initially I thought the error was inside the script, so to test it I called the clock by document.write( ). But then it worked fine! It is not what I need it but it shows that the script works (right?)...

Please give me your advise on this as well as any other comments on the script...

I have combined js, styles and html in one file here:

http://homepage.mac.co m/p_koumoundouros/.public/analogClock.htm l

Thank you in advance!
_______________ _______________ _______
Jul 30 '09 #1
9 14226
gits
5,390 Recognized Expert Moderator Expert
you call:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('theClock').innerHTML=clock;
instantly when the page is loaded ... and not when the document is ready ... so the dom cannot be used at this time. just add the innerHTML in the onload -event, for example like this:

wrap the line above in a function:

Expand|Select|Wrap|Line Numbers
  1. function initClock() {
  2.     document.getElementById('theClock').innerHTML=clock;
  3. }
then call:

Expand|Select|Wrap|Line Numbers
  1. <body onLoad="initClock(); displayClock();">
kind regards

PS: that is just a hack to fix it ... try to improve that code / flow of instructions :)
Jul 31 '09 #2
ipanos
4 New Member
Or a combination maybe?

Expand|Select|Wrap|Line Numbers
  1. function initClock() {
  2.    document.getElementById('theClock').innerHTML=clock;
  3.    displayClock();
  4. }
...and then only call the initClock in the body?

You are a star, hack || (!hack) it fixed the problem! I would appreciate a link to those instructions...

Thanks again!
Jul 31 '09 #3
gits
5,390 Recognized Expert Moderator Expert
with 'instruction' i meant the lines of code you write down ;) ... so there is no link. the basics for the solution of such problems are to know, that the document's DOM is ready to use when everything is loaded, parsed and built in memory of the browser ... and so a document's dom is reliably ready to use in the document's body's onload-handler, so when you use DOM-methods like getElementById( ) you need a readymade DOM-tree of the document ... which we have with our current solutions above :)

kind regards
Aug 1 '09 #4
ipanos
4 New Member
It all makes sense now....
Aug 1 '09 #5
gits
5,390 Recognized Expert Moderator Expert
:) ... i hope so ... just post back to the forum, anytime you have more questions ...

kind regards
Aug 2 '09 #6
ipanos
4 New Member
One more question has arrised:

Let's say I've got a small function like this:

Expand|Select|Wrap|Line Numbers
  1. function hideThis(id) {
  2.     var id=document.getElementById(id);
  3.     id.style.visibility='hidden'; }
to hide a <div>.

Is there some way when I call the function to be able to use more parameters, i.e make more than one <div>s to hide (all with one function)? What makes it difficult for me is that I am trying to do that for an undefined number of ids, i.e at some point I might call the function like "hideThis(id1,i d2,id3)" and at some other point "hideThis(id4,i d5)" (use a different number of parameters).

Thank you in advance!
Aug 7 '09 #7
gits
5,390 Recognized Expert Moderator Expert
of course ... just pass an array or object ... i show you an example with an array:

Expand|Select|Wrap|Line Numbers
  1. var l = ['id1', 'id2', 'id3'];
  2.  
  3. function hideSomething(list) {
  4.     for (var i = 0, l = list.length; i < l; ++i) {
  5.         var itemId = list[i];
  6.         document.getElementById(itemId).style.visibility = 'hidden';
  7.     }
  8. }
  9.  
  10. hideSomething(l);
  11.  
kind regards
Aug 7 '09 #8
Dormilich
8,658 Recognized Expert Moderator Expert
@ipanos
just a note: you might run into problems when you use a local variable with the same name as your parameter.
Aug 8 '09 #9
gits
5,390 Recognized Expert Moderator Expert
that's true :) ... i overlooked that ... when you put javascript in mozilla in strict mode the error console will give you a warning like:

'local variable hides argument ...'

or similar ... since you redeclare a variable that you already use as a parameter.

kind regards
Aug 8 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
46584
by: John Ramsden | last post by:
.... when the id 'junk' doesn't exist anywhere in the document, instead of returning 'object'?! I am using Javascript for a drop-down menu, slightly adapted from one by Angus Turnbull (see http://javascript.internet.com and http://gusnz.cjb.net, not that this is probably relevant but it deserves a plug ;-), on Internet Explorer v6.0.2800.1106. I need this feature of getElementById(), or an equivalent one (using sound and standard...
15
3284
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone tell me why these DIVs don't drift to the left as they are supposed to? <script language="javascript">
14
2036
by: drdeath89 | last post by:
Im writing some Javascript code that requires me to loop all child nodes and sub child nodes of a certain parent node and execute a function with the child node in question. I decided to write a recursive function with said functionality, with a node and a function as parameters, but it simply wont work. My problem is that whenever i pass a node to this function parameter it is always, regardless of what i change, equal to null. I even...
4
7488
by: Rob | last post by:
Hi, I have a label that I'm trying to make invisible on the client side using the OnClientClick event for a button. In the javascript function I have the following code: var x = getElementById("ctl00_ContentPlaceHolder1_label"); x.Visible = true;
2
1858
by: yogarajan | last post by:
hi i have create one aspx file with masterpage. i have using javascript code so in javascript code contains document.gelelementbyid but it gives error (document.getelementbyid is null or not an object) pls anyone give me the solutions
8
5626
by: ananjy | last post by:
Hi when i m trying to execute my ajax code using a link to display datas from another page named second.php., i got an error when the page loads in "ie"., which runs perfectly in firefox., here is my code <script type="text/javascript"> var i;
3
1690
by: phpmel | last post by:
hi guys, here is my problem. i have a js function called changemsg that takes a label in an aspx page and sets its text to blank. <script type="text/javascript" > function changemsg(lbl){ document.getElementById(lbl).innerHTML=" "; } </script>
29
19360
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my site and so far doing this has saved about 8KB of javascript (lots of ajax/dynamic elements). I just
2
3935
by: vegetable21 | last post by:
Hi, I'm relatively new to JavaScript, but not to programming. I'm trying to get a table to expand and collapse within a cell of my master table. However i want to collapse all the other open tables when i expand a new one. I'm nearly there but i get an error that i can't seem to shake. Firefox show me the error as the following: Error: document.getElementById("more" + i) is null Source File:...
0
11571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11344
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10690
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9886
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8254
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7429
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4945
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3541
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.