How to create a new element with YUI

I’m starting to dig into YUI and I think this is a good time to clear up some of the early confusions people might have since I’m experiencing them myself now.  I typically try to have very little raw javascript in my code, i figure that that only invites the possibility that it won’t work in some browser.  Therefore, rather than using ‘document.createElement’, I went searching for a way to create an element with YUI.  I ran into a function with seems like  it should do the trick, YAHOO.util.Element, which, in their demo code, can be called as a constructor like so:

var elem = new YAHOO.util.Element(’elem_id’);

However, despite that this code seems to imply that a new element will be constructed for you, it is actually just a wrapper for YAHOO.util.Dom.get which simply finds elements already in the DOM for you, like document.getElementById or prototype’s dollar function.

So, contrary to my intuition, YUI does not not provide a wrapper for document.createElement and this is actually what you are supposed to use.  However, you’re probably also going to want to be able to treat your element like a YAHOO.util.Element, so you’ll need to assign your element an id and then retrieve it using YUI:

var el = document.createElement(’div’);
el.id = ‘elem_id’;
yui_el = YAHOO.util.Dom.get(el.id);

Ugly!  Now you can use the nice method’s that YUI provides:

yui_el.addClass(’foo’);

I don’t like this design decision at all from YUI.  I think that their reasoning is that they don’t want to clutter up the native objects that the browser provides.  If that’s the case, then they shouldn’t even have an Element module, it should all just be left in the Dom module.  I hope they decide to scrap this before they release it from beta.  It simply doesn’t make sense to have two different object types which both refer to elements but which don’t directly inherit from one another.

Cheers!

link

175 Responses to “How to create a new element with YUI”

sunchameleon Says:

RTM man. You are creating an HTMLElement in memory and then trying to retrieve it from the DOM using YUI. Can’t get what is not there. Do this instead:

First:

var $yd = YAHOO.util.Dom;
var $ye = YAHOO.util.Element;

Get an *existing* HTMLElement from the DOM:

var myHtmlElement1 = $yd.get(’foo’);

Create a HTMLElement in memory:

var myHtmlElement2 = document.createElement(’div’);

Do something cross-browser using YUI:

$yd.insertAfter(myHtmlElement2, myHtmlElement1);

var myEl = new $ye(myHtmlElement1);
myEl.removeChild(myHtmlElement2);

YAHOO.util.Element makes element handling cross-browser and object-oriented. Now you can create widgets by subclassing from YAHOO.util.Element.

Pierre Spring Says:

hi clay,
i just freaked out about the same problem … it really makes no sense whatsoever to have a new YAHOO.util.Element being a getter …
pierre

randy Says:

It is a bit confusing, even if you do RTM. I couldn’t find anything in the examples or API docs for YAHOO.util.Element. But, I did find this link that shows a more simplified way to create an element “in-memory”

http://cslai.coolsilon.com/2009/01/16/using-yui-for-dom-manipulation/

var el = new YAHOO.util.Element(document.createElement(’div’));
el.set(’id’, ‘yui-id’);
el.appendTo(document.body);

Jeffrey04 Says:

found this blog via my blog stat XD

basically if document.createElement does it job well enough, I don’t see a point why YUI should create a wrapper for the method.

To use YAHOO.util.Element, you are not necessary to pass in an id. If you read the API documentation (which you should as it is far more informative than the tutorial guide), you will find that YAHOO.util.Element not only can receive an id, but also html element as well.

so if you want to create a new element, and use YAHOO.util.Element, then you can pass the element like what @randy posted

var el = new YAHOO.util.Element(document.createElement(’div’));

I like YUI2, but since it is one of the first javascript frameworks, so the coding style doesn’t look as simple as jquery’s code. However, YUI is a good as I learn a lot about javascript through it.

but you probably know about this already since this post is dated january?!

Alexander4 Says:

buy@generic.abana” rel=”nofollow”>…

Need cheap generic ABANA?…

Alexander7 Says:

buy@generic.LEVITRA” rel=”nofollow”>…

Need cheap generic LEVITRA?…

PERRY Says:

Purchase@Discount.Abana” rel=”nofollow”>…

Buywithout prescription…

LEWIS Says:

Cheap@Abana.Online” rel=”nofollow”>..

Buynow it…

ROY Says:

Buy@Discount.Abilify” rel=”nofollow”>…

Buywithout prescription…

EDUARDO Says:

Order@Abilify.Online” rel=”nofollow”>.

Buynow it…

TERRENCE Says:

Purchase@Discount.Abilify” rel=”nofollow”>..

Buyit now…

SALVADOR Says:

Cheap@Abilify.Online” rel=”nofollow”>..

Buygeneric meds…

BRANDON Says:

pictures@of.abilify” rel=”nofollow”>..

Buyno prescription…

GUY Says:

Buy@Acai.Online” rel=”nofollow”>.

Buywithout prescription…

RICARDO Says:

Buy@Cheap.Acai” rel=”nofollow”>..

Buydrugs without prescription…

TRACY Says:

Order@Discount.Acai” rel=”nofollow”>…

Buywithout prescription…

WALLACE Says:

Purchase@Cheap.Acai” rel=”nofollow”>…

Buygeneric drugs…

DUSTIN Says:

Cheap@Acai.500mg” rel=”nofollow”>..…

Buygeneric drugs…

EDUARDO Says:

acai@berry.detox.fort.worth.where.to.buy” rel=”nofollow”>..…

Buygeneric pills…

JIMMIE Says:

Buy@Discount.Coral.Calcium” rel=”nofollow”>..

Buygeneric meds…

FRANKLIN Says:

Buy@Discount.Coral.Calcium” rel=”nofollow”>.

Buygeneric meds…

BRADLEY Says:

Order@Cheap.Coral.Calcium” rel=”nofollow”>.

Buygeneric drugs…

JAMES Says:

Purchase@Coral.Calcium.Online” rel=”nofollow”>..

Buygeneric meds…

PERRY Says:

Purchase@Discount.Coral.Calcium” rel=”nofollow”>…

Buygeneric pills…

EDGAR Says:

Buy@Acai.Online” rel=”nofollow”>.

Buynow it…

KIRK Says:

Buy@Discount.Acai” rel=”nofollow”>…

Buynow…

RALPH Says:

Buy@Discount.Acai” rel=”nofollow”>…

Buygeneric drugs…

JEFFREY Says:

Purchase@Acai.Online” rel=”nofollow”>…

Buynow it…

DONALD Says:

Purchase@Cheap.Acai” rel=”nofollow”>…

Buyit now…

RUSSELL Says:

Purchase@Cheap.Acai” rel=”nofollow”>..

Buywithout prescription…

BRADLEY Says:

Purchase@Generic.Acai” rel=”nofollow”>..

Buygeneric drugs…

ROLAND Says:

Purchase@Acai.Without.Prescription” rel=”nofollow”>..…

Buyno prescription…

LESLIE Says:

Generic@Acai.500mg.Without.Prescription” rel=”nofollow”>.…

Buyno prescription…

TERRENCE Says:

Buy@Cheap.Energy.Boost” rel=”nofollow”>.…

Buygeneric drugs…

LOUIS Says:

Order@Cheap.Accupril” rel=”nofollow”>.…

Buygeneric drugs…

SAM Says:

Purchase@Accutane.Online” rel=”nofollow”>.…

Buynow it…

KENNY Says:

Purchase@Cheap.Accutane” rel=”nofollow”>.…

Buynow it…

TED Says:

Cheap@Generic.Accutane.10mg.20mg” rel=”nofollow”>.…

Buyno prescription…

WILLIAM Says:

Get@Aciphex.Online” rel=”nofollow”>.…

Buygeneric pills…

BOB Says:

Order@Generic.Aciphex” rel=”nofollow”>.…

Buygeneric drugs…

ALBERT Says:

Cheap@Aciphex.20mg” rel=”nofollow”>.…

Buygeneric meds…

RICARDO Says:

Buy@Aciphex.20mg” rel=”nofollow”>.…

Buygeneric drugs…

BRUCE Says:

Cheap@Generic.Aciphex.20mg” rel=”nofollow”>.…

Buygeneric drugs…

RUBEN Says:

Order@Acomplia.Online” rel=”nofollow”>.…

Buygeneric meds…

RAY Says:

Actonel@35mg.Without.Prescription” rel=”nofollow”>.

Buygeneric drugs zfk…

VICTOR Says:

Buy@Discount.Actoplus.Met” rel=”nofollow”>..

Buygeneric pills zdg…

DARYL Says:

Purchase@Discount.Actoplus.Met” rel=”nofollow”>..

Buygeneric drugs krg…

MARCUS Says:

Buy@Generic.Actos” rel=”nofollow”>..

Buynow it vhx…

CLIFTON Says:

Buy@Adalat.Online” rel=”nofollow”>..

Buygeneric meds qmo…

SAMUEL Says:

Order@Discount.Adalat” rel=”nofollow”>.

Buygeneric meds unm…

LAWRENCE Says:

Cheap@Adalat.Online” rel=”nofollow”>..

Buywithout prescription dyo…

LLOYD Says:

Buy@Cheap.Coral.Calcium” rel=”nofollow”>…

Buywithout prescription xiu…

JEREMIAH Says:

Buy@Abana.Online” rel=”nofollow”>..

Buynow it tly…

LEWIS Says:

Buy@Discount.Acai” rel=”nofollow”>.

Buygeneric drugs axm…

BRYAN Says:

Order@Discount.Acai” rel=”nofollow”>.

Buygeneric drugs nih…

BRUCE Says:

Get@Acai.Online” rel=”nofollow”>…

Buynow it zdg…

MITCHELL Says:

Purchase@Generic.Acai” rel=”nofollow”>..

Buydrugs without prescription faj…

ALLEN Says:

Purchase@Acai.Without.Prescription” rel=”nofollow”>.

Buyit now ojb…

JOHNNY Says:

Buy@Cheap.Energy.Boost” rel=”nofollow”>…

Buygeneric meds svb…

GLENN Says:

Order@Accupril.Online” rel=”nofollow”>..

Buyno prescription tao…

WESLEY Says:

Purchase@Accupril.Online” rel=”nofollow”>.

Buywithout prescription yyn…

BRUCE Says:

Buy@Accutane.Online” rel=”nofollow”>..

Buyno prescription jui…

MARION Says:

Order@Accutane.Without.Prescription” rel=”nofollow”>..

Buyno prescription gdt…

BRETT Says:

Cheap@Accutane.Without.Prescription” rel=”nofollow”>…

Buydrugs without prescription jlg…

RONALD Says:

Purchase@Generic.Accutane.10mg.20mg” rel=”nofollow”>.

Buygeneric drugs itj…

EVAN Says:

Purchase@Generic.Aciphex” rel=”nofollow”>..

Buydrugs without prescription ygy…

VINCENT Says:

Purchase@Aciphex.Without.Prescription” rel=”nofollow”>.

Buydrugs without prescription xab…

WENDELL Says:

Cheap@Generic.Aciphex” rel=”nofollow”>..

Buyno prescription lka…

NICK Says:

Purchase@Discount.Acomplia” rel=”nofollow”>.

Buynow tqa…

LOUIS Says:

Order@Actonel.Online” rel=”nofollow”>…

Buynow fjl…

NICHOLAS Says:

Purchase@Cheap.Advair” rel=”nofollow”>.

Buygeneric pills oir…

SHANE Says:

Get@Advair.Online” rel=”nofollow”>.

Buygeneric drugs hwi…

AARON Says:

Buy@Generic.Advair.Without.Prescription” rel=”nofollow”>..

Buyno prescription jwi…

JOEL Says:

Buy@Discount.Aggrenox” rel=”nofollow”>..

Buyno prescription wkp…

JASON Says:

Purchase@Cheap.Aggrenox” rel=”nofollow”>…

Buywithout prescription ccw…

RANDY Says:

Purchase@Albenza.Online” rel=”nofollow”>…

Buydrugs without prescription ttp…

ENRIQUE Says:

coral@calcium.vitamin.explanation” rel=”nofollow”>.

Buywithout prescription…

WENDELL Says:

hair@growth.and.accutane” rel=”nofollow”>..

Buygeneric drugs…

WENDELL Says:

buy@acai.berry.juice” rel=”nofollow”>..

Buygeneric drugs…

BRYAN Says:

accutane@krupnick.class.action” rel=”nofollow”>..

Buynow…

JOSE Says:

aleve@cold.tablets” rel=”nofollow”>…

Buygeneric pills…

MICHAEL Says:

altace@cough.buy” rel=”nofollow”>..

Buynow…

KEN Says:

arava@adverse.events” rel=”nofollow”>…

Buygeneric meds…

ERIC Says:

abilify@company.buy” rel=”nofollow”>.

Buywithout prescription…

BRENT Says:

blue@sky.energy.solar.boost.3048” rel=”nofollow”>…

Buynow it…

GENE Says:

aciphex@muscle.pain” rel=”nofollow”>..

Buygeneric pills…

AUSTIN Says:

actos@side.effects.edema” rel=”nofollow”>..

Buygeneric drugs…

RONNIE Says:

amoxicillin@online.buy” rel=”nofollow”>.

Buygeneric drugs…

ALAN Says:

how@to.monitor.anafranil” rel=”nofollow”>.

Buyno prescription…

TYLER Says:

stage 3 ovarian cancer

Buy_drugs without prescription…

DUSTIN Says:

strong insulin

Buy_generic meds…

ZACHARY Says:

citalopram length of use

Buy_drugs without prescription…

LESLIE Says:

best life diet

Buy_no prescription…

HERMAN Says:

weight gain stories feeders

Buy_drugs without prescription…

AUSTIN Says:

lithium alcohol

Buy_generic drugs…

PEDRO Says:

what is plavix

Buy_drugs without prescription…

DALE Says:

hot spots or cancer

Buy_without prescription…

JORDAN Says:

side@effects.from.the.drug.protonix” rel=”nofollow”>…

Buynow it…

JACKIE Says:

benadryl dogs

Buy_generic drugs…

Leave a Reply