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:
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:
el.id = ‘elem_id’;
yui_el = YAHOO.util.Dom.get(el.id);
Ugly! Now you can use the nice method’s that YUI provides:
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!
175 Responses to “How to create a new element with YUI”
April 7th, 2009 at 11:23 am
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.
April 29th, 2009 at 7:27 am
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
May 13th, 2009 at 10:49 pm
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);
August 15th, 2009 at 7:42 am
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?!
July 12th, 2011 at 8:05 pm
buy@generic.abana” rel=”nofollow”>……
Need cheap generic ABANA?…
July 19th, 2011 at 10:08 pm
buy@generic.LEVITRA” rel=”nofollow”>……
Need cheap generic LEVITRA?…
July 29th, 2011 at 7:55 am
Purchase@Discount.Abana” rel=”nofollow”>……
Buywithout prescription…
July 29th, 2011 at 8:20 am
Cheap@Abana.Online” rel=”nofollow”>..…
Buynow it…
July 29th, 2011 at 10:59 am
Buy@Discount.Abilify” rel=”nofollow”>……
Buywithout prescription…
July 29th, 2011 at 11:25 am
Order@Abilify.Online” rel=”nofollow”>.…
Buynow it…
July 29th, 2011 at 1:26 pm
Purchase@Discount.Abilify” rel=”nofollow”>..…
Buyit now…
July 29th, 2011 at 1:49 pm
Cheap@Abilify.Online” rel=”nofollow”>..…
Buygeneric meds…
July 29th, 2011 at 3:25 pm
pictures@of.abilify” rel=”nofollow”>..…
Buyno prescription…
July 29th, 2011 at 11:54 pm
Buy@Acai.Online” rel=”nofollow”>.…
Buywithout prescription…
July 30th, 2011 at 2:06 am
Buy@Cheap.Acai” rel=”nofollow”>..…
Buydrugs without prescription…
July 30th, 2011 at 9:05 am
Order@Discount.Acai” rel=”nofollow”>……
Buywithout prescription…
July 30th, 2011 at 12:45 pm
Purchase@Cheap.Acai” rel=”nofollow”>……
Buygeneric drugs…
July 31st, 2011 at 8:17 am
Cheap@Acai.500mg” rel=”nofollow”>..…
Buygeneric drugs…
July 31st, 2011 at 9:05 am
acai@berry.detox.fort.worth.where.to.buy” rel=”nofollow”>..…
Buygeneric pills…
July 31st, 2011 at 11:27 pm
Buy@Discount.Coral.Calcium” rel=”nofollow”>..…
Buygeneric meds…
August 2nd, 2011 at 12:10 am
Buy@Discount.Coral.Calcium” rel=”nofollow”>.…
Buygeneric meds…
August 2nd, 2011 at 1:56 am
Order@Cheap.Coral.Calcium” rel=”nofollow”>.…
Buygeneric drugs…
August 2nd, 2011 at 3:05 am
Purchase@Coral.Calcium.Online” rel=”nofollow”>..…
Buygeneric meds…
August 2nd, 2011 at 4:13 am
Purchase@Discount.Coral.Calcium” rel=”nofollow”>……
Buygeneric pills…
August 3rd, 2011 at 11:38 am
Buy@Acai.Online” rel=”nofollow”>.…
Buynow it…
August 3rd, 2011 at 3:06 pm
Buy@Discount.Acai” rel=”nofollow”>……
Buynow…
August 3rd, 2011 at 3:37 pm
Buy@Discount.Acai” rel=”nofollow”>……
Buygeneric drugs…
August 3rd, 2011 at 9:50 pm
Purchase@Acai.Online” rel=”nofollow”>……
Buynow it…
August 3rd, 2011 at 10:10 pm
Purchase@Cheap.Acai” rel=”nofollow”>……
Buyit now…
August 3rd, 2011 at 11:36 pm
Purchase@Cheap.Acai” rel=”nofollow”>..…
Buywithout prescription…
August 4th, 2011 at 5:40 pm
Purchase@Generic.Acai” rel=”nofollow”>..…
Buygeneric drugs…
August 4th, 2011 at 7:17 pm
Purchase@Acai.Without.Prescription” rel=”nofollow”>..…
Buyno prescription…
August 4th, 2011 at 10:09 pm
Generic@Acai.500mg.Without.Prescription” rel=”nofollow”>.…
Buyno prescription…
August 4th, 2011 at 11:27 pm
Buy@Cheap.Energy.Boost” rel=”nofollow”>.…
Buygeneric drugs…
August 5th, 2011 at 2:44 pm
Order@Cheap.Accupril” rel=”nofollow”>.…
Buygeneric drugs…
August 6th, 2011 at 5:18 am
Purchase@Accutane.Online” rel=”nofollow”>.…
Buynow it…
August 6th, 2011 at 6:25 am
Purchase@Cheap.Accutane” rel=”nofollow”>.…
Buynow it…
August 6th, 2011 at 11:35 pm
Cheap@Generic.Accutane.10mg.20mg” rel=”nofollow”>.…
Buyno prescription…
August 7th, 2011 at 5:43 pm
Get@Aciphex.Online” rel=”nofollow”>.…
Buygeneric pills…
August 7th, 2011 at 9:48 pm
Order@Generic.Aciphex” rel=”nofollow”>.…
Buygeneric drugs…
August 8th, 2011 at 10:38 am
Cheap@Aciphex.20mg” rel=”nofollow”>.…
Buygeneric meds…
August 8th, 2011 at 12:00 pm
Buy@Aciphex.20mg” rel=”nofollow”>.…
Buygeneric drugs…
August 8th, 2011 at 3:11 pm
Cheap@Generic.Aciphex.20mg” rel=”nofollow”>.…
Buygeneric drugs…
August 9th, 2011 at 3:55 am
Order@Acomplia.Online” rel=”nofollow”>.…
Buygeneric meds…
August 10th, 2011 at 7:18 pm
Actonel@35mg.Without.Prescription” rel=”nofollow”>.…
Buygeneric drugs zfk…
August 10th, 2011 at 8:58 pm
Buy@Discount.Actoplus.Met” rel=”nofollow”>..…
Buygeneric pills zdg…
August 11th, 2011 at 12:18 am
Purchase@Discount.Actoplus.Met” rel=”nofollow”>..…
Buygeneric drugs krg…
August 11th, 2011 at 8:37 am
Buy@Generic.Actos” rel=”nofollow”>..…
Buynow it vhx…
August 12th, 2011 at 7:44 am
Buy@Adalat.Online” rel=”nofollow”>..…
Buygeneric meds qmo…
August 12th, 2011 at 12:22 pm
Order@Discount.Adalat” rel=”nofollow”>.…
Buygeneric meds unm…
August 12th, 2011 at 2:27 pm
Cheap@Adalat.Online” rel=”nofollow”>..…
Buywithout prescription dyo…
August 12th, 2011 at 4:35 pm
Buy@Cheap.Coral.Calcium” rel=”nofollow”>……
Buywithout prescription xiu…
August 12th, 2011 at 10:01 pm
Buy@Abana.Online” rel=”nofollow”>..…
Buynow it tly…
August 14th, 2011 at 12:48 am
Buy@Discount.Acai” rel=”nofollow”>.…
Buygeneric drugs axm…
August 14th, 2011 at 3:59 am
Order@Discount.Acai” rel=”nofollow”>.…
Buygeneric drugs nih…
August 14th, 2011 at 9:40 am
Get@Acai.Online” rel=”nofollow”>……
Buynow it zdg…
August 14th, 2011 at 4:17 pm
Purchase@Generic.Acai” rel=”nofollow”>..…
Buydrugs without prescription faj…
August 14th, 2011 at 5:18 pm
Purchase@Acai.Without.Prescription” rel=”nofollow”>.…
Buyit now ojb…
August 15th, 2011 at 6:49 am
Buy@Cheap.Energy.Boost” rel=”nofollow”>……
Buygeneric meds svb…
August 15th, 2011 at 9:11 pm
Order@Accupril.Online” rel=”nofollow”>..…
Buyno prescription tao…
August 16th, 2011 at 5:24 am
Purchase@Accupril.Online” rel=”nofollow”>.…
Buywithout prescription yyn…
August 16th, 2011 at 4:52 pm
Buy@Accutane.Online” rel=”nofollow”>..…
Buyno prescription jui…
August 17th, 2011 at 9:18 am
Order@Accutane.Without.Prescription” rel=”nofollow”>..…
Buyno prescription gdt…
August 17th, 2011 at 1:49 pm
Cheap@Accutane.Without.Prescription” rel=”nofollow”>……
Buydrugs without prescription jlg…
August 17th, 2011 at 6:08 pm
Purchase@Generic.Accutane.10mg.20mg” rel=”nofollow”>.…
Buygeneric drugs itj…
August 18th, 2011 at 6:21 pm
Purchase@Generic.Aciphex” rel=”nofollow”>..…
Buydrugs without prescription ygy…
August 18th, 2011 at 7:38 pm
Purchase@Aciphex.Without.Prescription” rel=”nofollow”>.…
Buydrugs without prescription xab…
August 18th, 2011 at 8:48 pm
Cheap@Generic.Aciphex” rel=”nofollow”>..…
Buyno prescription lka…
August 20th, 2011 at 1:23 am
Purchase@Discount.Acomplia” rel=”nofollow”>.…
Buynow tqa…
August 21st, 2011 at 10:34 am
Order@Actonel.Online” rel=”nofollow”>……
Buynow fjl…
August 23rd, 2011 at 5:27 am
Purchase@Cheap.Advair” rel=”nofollow”>.…
Buygeneric pills oir…
August 23rd, 2011 at 10:47 am
Get@Advair.Online” rel=”nofollow”>.…
Buygeneric drugs hwi…
August 23rd, 2011 at 2:47 pm
Buy@Generic.Advair.Without.Prescription” rel=”nofollow”>..…
Buyno prescription jwi…
August 24th, 2011 at 4:50 pm
Buy@Discount.Aggrenox” rel=”nofollow”>..…
Buyno prescription wkp…
August 24th, 2011 at 11:30 pm
Purchase@Cheap.Aggrenox” rel=”nofollow”>……
Buywithout prescription ccw…
August 25th, 2011 at 12:50 pm
Purchase@Albenza.Online” rel=”nofollow”>……
Buydrugs without prescription ttp…
August 26th, 2011 at 3:15 am
coral@calcium.vitamin.explanation” rel=”nofollow”>.…
Buywithout prescription…
August 26th, 2011 at 7:15 am
hair@growth.and.accutane” rel=”nofollow”>..…
Buygeneric drugs…
August 26th, 2011 at 3:14 pm
buy@acai.berry.juice” rel=”nofollow”>..…
Buygeneric drugs…
August 26th, 2011 at 5:54 pm
accutane@krupnick.class.action” rel=”nofollow”>..…
Buynow…
August 27th, 2011 at 4:35 am
aleve@cold.tablets” rel=”nofollow”>……
Buygeneric pills…
August 27th, 2011 at 1:54 pm
altace@cough.buy” rel=”nofollow”>..…
Buynow…
August 27th, 2011 at 7:37 pm
arava@adverse.events” rel=”nofollow”>……
Buygeneric meds…
August 27th, 2011 at 11:14 pm
abilify@company.buy” rel=”nofollow”>.…
Buywithout prescription…
August 28th, 2011 at 5:55 am
blue@sky.energy.solar.boost.3048” rel=”nofollow”>……
Buynow it…
August 28th, 2011 at 9:52 am
aciphex@muscle.pain” rel=”nofollow”>..…
Buygeneric pills…
August 28th, 2011 at 12:14 pm
actos@side.effects.edema” rel=”nofollow”>..…
Buygeneric drugs…
August 29th, 2011 at 1:35 am
amoxicillin@online.buy” rel=”nofollow”>.…
Buygeneric drugs…
August 29th, 2011 at 4:26 am
how@to.monitor.anafranil” rel=”nofollow”>.…
Buyno prescription…
October 16th, 2011 at 7:32 pm
herpes kelly dating kansas…
Buy_no prescription…
October 17th, 2011 at 1:59 pm
stage 3 ovarian cancer…
Buy_drugs without prescription…
October 18th, 2011 at 7:55 am
strong insulin…
Buy_generic meds…
October 18th, 2011 at 11:15 am
antidepressant drugs for cats…
Buy_generic drugs…
October 18th, 2011 at 12:55 pm
alberta alcohol and drug abuse commission…
Buy_generic meds…
October 18th, 2011 at 5:55 pm
the process of methanogens metabolism…
Buy_drugs without prescription…
October 19th, 2011 at 3:55 am
erectile dysfunction herbal treatment…
Buy_drugs without prescription…
October 19th, 2011 at 7:15 am
brown algae good for slow thyroid…
Buy_generic drugs…
October 19th, 2011 at 5:17 pm
drug detection testing…
Buy_generic meds…
October 20th, 2011 at 1:17 am
recipes for gastric bypass patients…
Buy_now…
October 20th, 2011 at 12:56 pm
citalopram length of use…
Buy_drugs without prescription…
October 20th, 2011 at 10:56 pm
does testosterone make you gain weight…
Buy_generic drugs…
October 21st, 2011 at 9:27 am
best life diet…
Buy_no prescription…
October 21st, 2011 at 9:07 pm
weight gain stories feeders…
Buy_drugs without prescription…
October 22nd, 2011 at 5:27 am
accutane birth defects…
Buy_it now…
October 23rd, 2011 at 12:23 am
lithium alcohol…
Buy_generic drugs…
October 23rd, 2011 at 2:04 am
menstrual patterns in menopause…
Buy_no prescription…
October 23rd, 2011 at 7:03 am
market drugs edmonton…
Buy_now it…
October 24th, 2011 at 2:09 pm
dependent disorder anxiety…
Buy_generic pills…
October 24th, 2011 at 5:28 pm
adrenal cancer symptoms…
Buy_generic drugs…
October 24th, 2011 at 8:48 pm
green nerf ds lite armour…
Buy_generic pills…
October 25th, 2011 at 3:08 pm
embed video webpage…
Buy_generic drugs…
October 25th, 2011 at 9:48 pm
indigestion and chest pain…
Buy_now it…
October 26th, 2011 at 6:09 am
herbal flee collars for dogs…
Buy_generic pills…
October 26th, 2011 at 7:48 am
ultimate diet pills…
Buy_generic pills…
October 26th, 2011 at 4:08 pm
what is plavix…
Buy_drugs without prescription…
October 26th, 2011 at 10:48 pm
postpartum remeron…
Buy_no prescription…
October 27th, 2011 at 2:09 am
statistics on pregnancy in high school…
Buy_generic meds…
October 27th, 2011 at 5:08 pm
medication errors 2nd ed…
Buy_no prescription…
October 28th, 2011 at 1:07 pm
what is valtrex used for…
Buy_generic drugs…
October 29th, 2011 at 6:21 pm
decongestants hypertension safe…
Buy_now it…
October 29th, 2011 at 11:21 pm
cures for fingernail fungi…
Buy_generic meds…
October 30th, 2011 at 3:27 am
symptoms of chocolate allergy…
Buy_it now…
October 30th, 2011 at 3:05 pm
prozac daily strength…
Buy_generic pills…
October 30th, 2011 at 9:45 pm
acne in cats…
Buy_it now…
October 30th, 2011 at 11:25 pm
canine kidney failure symptoms…
Buy_generic pills…
October 31st, 2011 at 1:05 am
deadliest type of cancer…
Buy_generic pills…
October 31st, 2011 at 7:45 am
bacterial staphylococcus aureus meningitis symptoms…
Buy_without prescription…
October 31st, 2011 at 2:25 pm
genie francis weight loss…
Buy_now…
October 31st, 2011 at 9:05 pm
nursing times alzheimer mavis ford…
Buy_it now…
November 1st, 2011 at 3:45 am
st joseph aspirin coupon…
Buy_generic pills…
November 1st, 2011 at 8:45 am
diseaeses contracted from sand fleas…
Buy_now it…
November 1st, 2011 at 12:05 pm
insulin receptor inhibition…
Buy_without prescription…
November 1st, 2011 at 6:45 pm
pet ct scan results accuracy cancer…
Buy_drugs without prescription…
November 2nd, 2011 at 1:25 am
pictures of herpes blisters…
Buy_generic pills…
November 2nd, 2011 at 6:25 am
persistent nausea and stomach pain…
Buy_generic meds…
November 2nd, 2011 at 6:05 pm
cordarone intravenous…
Buy_generic meds…
November 2nd, 2011 at 7:45 pm
respiratory infection humidity dry antibiotics avelox…
Buy_generic pills…
November 2nd, 2011 at 9:25 pm
find clomid cheap in us…
Buy_now…
November 3rd, 2011 at 7:53 pm
reversing alcohol induced neuropathy…
Buy_generic meds…
November 4th, 2011 at 12:53 am
sample tlc diets…
Buy_generic drugs…
November 4th, 2011 at 5:53 am
natural cures for premature ejaculation…
Buy_generic drugs…
November 5th, 2011 at 6:10 am
hot spots or cancer…
Buy_without prescription…
November 5th, 2011 at 9:30 am
canadian paediatric diabetes association…
Buy_now…
November 5th, 2011 at 10:50 pm
south beach diet foods to avoid…
Buy_no prescription…
November 6th, 2011 at 2:10 am
discovery of zinc in human health…
Buy_now…
November 6th, 2011 at 5:30 am
rebound phenomenon during gradual clonidine withdrawal…
Buy_drugs without prescription…
November 6th, 2011 at 10:30 am
what causes constant itching…
Buy_generic pills…
November 6th, 2011 at 5:10 pm
emotions surviveing uterine cancer…
Buy_generic meds…
November 6th, 2011 at 6:50 pm
washington university marfan losartan study…
Buy_drugs without prescription…
November 6th, 2011 at 10:10 pm
clinical trial ediary data…
Buy_now it…
November 7th, 2011 at 7:50 pm
100 mile diet store…
Buy_generic meds…
November 7th, 2011 at 9:30 pm
juliet’s on the spot acne treatment…
Buy_drugs without prescription…
November 8th, 2011 at 11:44 am
pain relief until root canal…
Buy_generic meds…
November 8th, 2011 at 1:24 pm
diet generator reviews…
Buy_it now…
November 8th, 2011 at 8:04 pm
psychiatric care model…
Buy_generic meds…
November 9th, 2011 at 7:44 am
glucophage powered by vbulletin version 2.2.1…
Buy_generic pills…
November 9th, 2011 at 10:44 pm
neurontin and mood lability…
Buy_generic meds…
November 10th, 2011 at 3:44 am
menses while on birth control pills…
Buy_generic drugs…
November 10th, 2011 at 3:24 pm
interactions with azithromycin and astragalus…
Buy_it now…
November 11th, 2011 at 1:24 am
most successful diet ever…
Buy_generic pills…
November 11th, 2011 at 2:44 pm
medical nebulizer…
Buy_generic meds…
November 11th, 2011 at 11:04 pm
dog retching vomiting weight loss…
Buy_generic drugs…
November 12th, 2011 at 2:24 am
sleep apnea side effect of lexapro…
Buy_generic pills…
November 12th, 2011 at 4:04 am
does hiv always lead to aids…
Buy_generic meds…
November 13th, 2011 at 1:44 am
space herpes…
Buy_now…
November 13th, 2011 at 4:44 pm
can iodine help thyroid function…
Buy_generic drugs…
November 13th, 2011 at 11:24 pm
breven medication for add…
Buy_generic drugs…
November 15th, 2011 at 8:42 pm
drug citalopram side effects…
Buy_generic drugs…
November 15th, 2011 at 10:22 pm
information about treatment for leg edema…
Buy_generic drugs…
November 16th, 2011 at 10:02 am
remeron dreams…
Buy_it now…
November 16th, 2011 at 11:22 pm
fruity taste and diabetes…
Buy_it now…
November 17th, 2011 at 1:02 am
dogs to detect cancer…
Buy_generic drugs…
December 9th, 2011 at 5:28 am
side@effects.from.the.drug.protonix” rel=”nofollow”>……
Buynow it…
December 11th, 2011 at 2:05 am
benadryl dogs…
Buy_generic drugs…
January 6th, 2012 at 5:20 pm
advair diskus…
advair diskus…
Leave a Reply