[ 3 posts ]

soon0009

  • Joined: Tue Aug 11, 2009 6:23 pm
  • Posts: 4
  • Offline
  • Profile
Tags:

Why does YUI.util.Dom.get return a Element reference?

Post Posted: Mon May 24, 2010 6:15 pm
+0-
Hi,

I was wondering why is that when I use YUI.util.Dom.get I get a reference to a element and not the element itself.

Here is the situation where this came up:
Code:
  // this will not work
  YAHOO.util.Dom.get("some_element").addClass("some_class");

  // this wil' work
  YAHOO.util.Dom.addClass("some_element", "some_class");


Thanks,

Nigel

Caridy Patino

YUI Contributor

  • Username: caridy
  • Joined: Mon Dec 08, 2008 5:40 pm
  • Posts: 493
  • Location: Miami, FL
  • Twitter: caridy
  • GitHub: caridy
  • Gists: caridy
  • IRC: caridy
  • YUI Developer
  • Offline
  • Profile

Re: Why does YUI.util.Dom.get return a Element reference?

Post Posted: Tue May 25, 2010 5:25 am
+0-
Hello Nigel,

In Javascript there is not object itself, what you get is always a reference to an object, even when you do this:

var a = {};

Anyway, that's not the problem here. DOM "get" returns a reference to a DOM element, and "addClass" is not a DOM method, that's why you cannot use the chain approach with it.

In the case of the second sentence, DOM "addClass" is a YUI 2 implementation. It uses "get" to collect the reference, then apply a regular expression to see if that class is already applied, if not, it will add it to the class attribute.

That said, YUI 3 uses a different approach, Y.one, Y.all and all the other methods that interact with DOM elements use a NODE object instead of a DOM element, which is a YUI 3 implementation to encapsulate a DOM element, and that's why you can do chaining:

Code:
Y.one('#somelement') .addClass('some_class');


Best Regards,
Caridy

soon0009

  • Joined: Tue Aug 11, 2009 6:23 pm
  • Posts: 4
  • Offline
  • Profile

Re: Why does YUI.util.Dom.get return a Element reference?

Post Posted: Tue May 25, 2010 7:14 am
+0-
Great, thanks for the Javascript lesson! :)
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum