getElementById

Returning null?

Page 1 of 1

2 Replies - 625 Views - Last Post: 12 October 2009 - 04:53 PM

#1 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

getElementById

Posted 12 October 2009 - 03:06 PM

So I need to problematically add a button to a little button bar. The basic technique I am trying to use is:

var buttonbar = document.getElementById("buttonbar");
if (buttonbar) {
	var myButtonHTML = "...";
	console.log("Adding custom button");
	var html = myButtonHTML + pagemenu.innerHTML;
	buttonbar.innerHTML = html;
} else {
   console.log("Could not locate button bar";
}
var buttonbar = null;



The button bar is in the generated HTML as:
<td id="buttonbar" class="Items_buttonbarGrid">
	<div style="display: inline;" id="divEditItem">
		<a onclick="java script:somefunction();" href="#" id="editItem">
			<img width="16" height="16" border="0" class="icon" title="Edit Item" src="/images/icon_edit_16.gif"/>
		</a>
	</div>
	...
</td>


Now the problem is that I can't get the button bar! I know it is there as I can see it, FF's Web Developer plugin shows it there -- so does FireBug... however when I try to use:
var buttonbar = document.getElementById("buttonbar"); I get null -- even in the firebug console even though I can see it in the DOM...

am I missing something stupid? I am not really used to pages that are generated from so many different components -- can my script only see those elements it creates or something?

Is This A Good Question/Topic? 0
  • +

Replies To: getElementById

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: getElementById

Posted 12 October 2009 - 03:32 PM

Well a little debugging showed that the element was not available at the time it was run -- so I have a problem: how can I tell when the element is available?

I tried adding this into a function:

dojo.addonload(function() { .. });

but still the element is not available...
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: getElementById

Posted 12 October 2009 - 04:53 PM

Fixed... the problem was that the element "buttonbar" was loading via an ajax call and so was not part of the document until after the call. Since this a dojo application I had to override the component's onload() function and add in a hook to my code.

I am still not sure why it element was not available in firebug -- perhaps I don't understand how to use firebug? Or perhaps it has something to do with the element being dynamically loaded into the DOM by the Dojo framework... (oddly enough the dojo object is also not available in the firebug console)... perhaps since this is proprietary software the boys in engineering have some neat bit of code which hinders firebug (but since their own documentation recommends we use firebug I think that is unlikely).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1