Jump to content

Is it possible to add a method to the DOM document?

Recommended Posts

I am developing a function to find a particular element within another element identifier (similar to getElementById but that applies to any other element) ... Because this throws me arror:

var ancestor = document.getElementById('divAncestor');var child = ancestor.getElementById('divChild');

I know, HTML specifies that ids must be unique but reality proves that often running scripts that can have items with the same ids. So, thats the reason why I'm not simply calling the child like this:

var ancestor = document.getElementById('divChild');

The first option was to add this function to each HTML element, or at least the divs (because that's what interests me identify). The second, and is the one I prefer, is to add a method to the document object and call from there by passing the ID of the father and son as parameters. Is this possible?Thanks for your time.

Edited by gal007
Link to post
Share on other sites

It's probably possible, but it's never required or a good idea to have multiple elements with the same ID. If you think you need that, then your design isn't good. You never need multiple elements with the same ID, there is always a way around that.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...