faqts : Computers : Programming : Languages : JavaScript : XML : E4X (ECMAScript for XML) : XML objects

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

Entry

How do I use the method insertChildAfter?
How do I use the method insertChildAfter?

Apr 11th, 2005 01:29
Martin Honnen,


The method insertChildAfter takes two arguments, an existing child to
insert after and the new child to be inserted. 
If the first argument is null then the second argument is inserted as
the first child of the XML object the method is called on. Otherwise the
second argument is inserted directly after the first argument if that
exists as a child or not at all. 
If the second argument is inserted successfully then the XML object the
method is called on is returned, otherwise undefined is returned.
Note also that in E4X neither attributes nor text nodes nor processing
instructions nor comments can have child nodes so when called on such
XML objects the method does nothing and returns undefined.
Here are some examples:
  var gods = <gods>
    <god>Kibo</god>
    <god>Xibo</god>
  </gods>;
  // insert as first child
  gods.insertChildAfter(null, <god>Maho</god>);
  alert(gods);
  /* shows
  '<gods>
     <god>Maho</god>
     <god>Kibo</god>
     <god>Xibo</god>
   </gods>'
  */
  gods.insertChildAfter(gods.*[2], <god>Jaffo</god>);
  alert(gods);
  /* shows
  '<gods>
     <god>Maho</god>
     <god>Kibo</god>
     <god>Xibo</god>
     <god>Jaffo</god>
   </gods>'
  */
  // child to insert after does not exist:
  var result = gods.insertChildAfter(gods.god[200], <god>Buffo</god>);
  alert(result); // shows 'undefined'
  alert(gods);
  /* shows unchanged
  '<gods>
     <god>Maho</god>
     <god>Kibo</god>
     <god>Xibo</god>
     <god>Jaffo</god>
   </gods>'
  */