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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access