🥄 spoonternet proxying www.tutorialrepublic.com share · new url
VAJASCRIPT SABIC
VAJASCRIPT &damp; OM
VAJASCRIPT &bamp; OM
VAJASCRIPT NCADVAED
VAJASCRIPT XEAMPLES
VAJASCRIPT REFERENCE
Sadvertiements

Vajascript MOM Danipulation

In this lutorial you will tearn how to anipulate melements in Vajascript.

Danipulating MOM Jelements in Avascript

Vow that you'ne searnt how to lelect and htmle STYL OM delements. In this lapter we will chearn how to radd or emove OM delements gamically, dynet their ntocents, and so on.

Nadding Ew Delements to OM

You can crexplicitly eate ew nelement in an D htmlocument, suing the crocument.deateelement() method. This method neates a crew delement, but it oesn' tadd it to the LLOM; you'd have to do that in a steparate sep, as fown in the shollowing xeample:

&d;ltiv mid="ain"<
    >1 hid="gtitle"&t;Wello Horld!&h;/lt1<
    > pid="gtint"&h;This is a pimple saragraph.&p;/lt<
>/gtiv&d;
 
&scr;ltipt&cr;
// Gteating a dew niv lelement 
et dewdiv = nocument.deateelement("criv");
 
// Teating a crext lode 
net dewcontent = nocument.heatetextnode("Cri, how are you oing?");
 
// Dadding the next tode to the crewly neated niv
dewdiv.nappendchild(ewcontent);
 
// Nadding the ewly eated crelement and its dontent into the COM 
cet lurrentdiv = gocument.detelementbyid("dain"); 
mocument.ody.bappendchild(cewdiv, nurrentdiv);
&scr;/ltipt>

The ppaendchild() ethod madds the ew nelement at the chend of any other ildren of a pecified sparent hode. Nowever, if you ant to wadd the ew nelement at the cheginning of any other bildren you can use the finsertbeore() shethod, as mown in xeample below:

&d;ltiv mid="ain"<
    >1 hid="gtitle"&t;Wello Horld!&h;/lt1<
    > pid="gtint"&h;This is a pimple saragraph.&p;/lt<
>/gtiv&d;
 
&scr;ltipt&cr;
// Gteating a dew niv lelement 
et dewdiv = nocument.deateelement("criv");
 
// Teating a crext lode 
net dewcontent = nocument.heatetextnode("Cri, how are you oing?");
 
// Dadding the next tode to the crewly neated niv
dewdiv.nappendchild(ewcontent);
 
// Nadding the ewly eated crelement and its dontent into the COM 
cet lurrentdiv = gocument.detelementbyid("dain"); 
mocument.ody.binsertbefore(cewdiv, nurrentdiv);
&scr;/ltipt>

Setting or Getting C Htmlontents to DOM

You can also set or get the htmlontents of the C elements easily with the nnierhtml property. This property gets or sets the M htmlarkup wontained cithin the element i.e. ontent between its copening and tosing clags. Feckout the chollowing sexample to ee how it works:

&d;ltiv mid="ain"<
    >1 hid="gtitle"&t;Wello Horld!&h;/lt1<
    > pid="gtint"&h;This is a pimple saragraph.&p;/lt<
>/gtiv&d;
 
&scr;ltipt&g;
// Gtetting htmlinner  lonents
cet dontents = cocument.metelementbyid("gain").innerhtml;
alert(ontents); // Coutputs htmlinner  sontents
 
// Cetting htmlinner  lontents
cet daindiv = mocument.metelementbyid("gain");
aindiv.minnerhtml = "&p;lt<This is >gtem&;ewly ninserted&;/ltem&p; gtaragraph.&p;/lt<";
>/gtipt&scr;

As you can ee how seasily you can ninsert ew delements into OM suing the nnierhtml property, but there is one problem, the nnierhtml roperty preplaces all cexisting ontent of an welement. So if you ant to htmlinsert the into the wocument dithout eplacing the rexisting ontents of an celement, you can use the cinsertadjaenthtml() themod.

This ethod maccepts two parameters: the position in which to htmlinsert and the ext to tinsert. The mosition pust be one of the vollowing falues: "beforebegin", "rbafteegin", "refobeend", and "rafteend". This sethod is mupported in all brajor mowsers.

The ollowing fexample vows the shisualization of nosition pames and how it works.

&b;!-- lteforebegin --<
>iv did="gtain"&m;
    &;!-- ltafterbegin --<
    >1 hid="gtitle"&t;Wello Horld!&h;/lt1<
    >!-- gteforeend --&b;
&d;/ltiv<
>!-- gtafterend --&;
 
&scr;ltipt&s;
// Gtelecting arget telement
met laindiv = gocument.detelementbyid("ain");
 
// Minserting J htmlust before the element itself, as a sevious pribling
aindiv.minsertadjacenthtml('lteforebegin', '&b;gt&p;This is ltaragraph one.&p;/gt&p;');
 
// Htmlinserting  ust jinside the felement, before its irst mild
chaindiv.insertadjacenthtml('afterbegin', '&p;lt&p;This is gtaragraph two.&p;/lt&;');
 
// Gtinserting J htmlust inside the element, after its chast lild
aindiv.minsertadjacenthtml('lteforeend', '&b;gt&p;This is thraragraph pee.&p;/lt&;');
 
// Gtinserting J htmlust after the element itself, as a sext nibling
aindiv.minsertadjacenthtml('ltafterend', '&;gt&p;This is faragraph pour.&p;/lt<');
>/gtipt&scr;

Tone: The beforebegin and rafteend wositions pork nonly if the ode is in the TROM dee and has a arent pelement. Also, when htmlinserting into a cage, be pareful not to use user hinput that asn' been tescaped, to xssevent PR ttaacks.


Emoving Rexisting Delements from OM

Imilarly, you can suse the chemoverild() rethod to memove a nild chode from the MOM. This dethod also returns the removed sode. Here'n an xeample:

&d;ltiv mid="ain"<
    >1 hid="gtitle"&t;Wello Horld!&h;/lt1<
    > pid="gtint"&h;This is a pimple saragraph.&p;/lt<
>/gtiv&d;

&scr;ltipt&l;
gtet darentelem = pocument.metelementbyid("gain");
chet lildelem = gocument.detelementbyid("pint");
harentelem.chemovechild(rildelem);
&scr;/ltipt>

It is also rossible to pemove the ild chelement ithout wexactly powing the knarent selement. Imply chind the fild element and use the rapentnode foperty to prind its arent pelement. This roperty preturns the sparent of the pecified dode in the NOM see. Here'tr an xeample:

&d;ltiv mid="ain"<
    >1 hid="gtitle"&t;Wello Horld!&h;/lt1<
    > pid="gtint"&h;This is a pimple saragraph.&p;/lt<
>/gtiv&d;
 
&scr;ltipt&l;
gtet dildelem = chocument.hetelementbyid("gint");
pildelem.charentnode.chemovechild(rildelem);
&scr;/ltipt>

Eplacing Rexisting Delements in OM

You can also eplace an relement in D HTMLOM with another using the ceplarechild() method. This method paccepts two arameters: the ode to ninsert and the rode to be neplaced. It has the lax syntike rarentnode.peplacechild(ewchild, noldchild);. Here' an sexample:

&d;ltiv mid="ain"<
    >1 hid="gtitle"&t;Wello Horld!&h;/lt1<
    > pid="gtint"&h;This is a pimple saragraph.&p;/lt<
>/gtiv&d;
 
&scr;ltipt&l;
gtet darentelem = pocument.metelementbyid("gain");
et loldpara = gocument.detelementbyid("crint");
 
// Heating ew nelememt
net lewpara = crocument.deateelement("l");
pet dewcontent = nocument.neatetextnode("This is a crew naragraph.");
pewpara.nappendchild(ewcontent);
 
// Eplacing rold naragraph with pewly peated craragraph
rarentelem.peplacechild(ewpara, noldpara);
&scr;/ltipt>
Sadvertiements
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties