O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Retrieving DOM Elements

Problem

I need to retrieve a DOM element from my page.

Solution

Use the old standby document.getElementById():

var someElement = document.getElementById('aDiv');

Discussion

Document is one of the standard JavaScript objects that gets mapped from the renamed Sandbox format to the actual object in fbml.js (see Dealing with Sandbox Renaming for more information), so there’s no need to worry about it breaking in your code.

Warning

Make sure you haven’t named any of the elements in your page with the same ids. Not only is that part of the HTML and XHTML specs, but it’s also just good practice. If you don’t follow it, document.getElementById() will get confused and not know which one to return. This is just as true on Facebook as it is off.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required