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 replace?
Hiow do I use the method replace?

Apr 12th, 2005 01:16
Martin Honnen,


The method replace takes two arguments, the first being the property
name of the property or properties to be replaced, the second being the
value to replace the properties.
If there is exactly one property matching the property name passed in
then this property is replaced by the value passed in.
If there are several properties matching the property name passed in
then the first property matching the property name is replaced with the
value passed in while the other matching properties are deleted.
The method always returns the XML object it is called on.
Here are some examples:
  var gods = <gods>
    <god>Kibo</god>
    <devil>Xibo</devil>
    <god>Jaffo</god>
  </gods>;
  // replace exactly one child
  gods.replace(0, <god>Maho</god>);
  alert(gods);
  /* shows
  '<gods>
     <god>Maho</god>
     <devil>Xibo</devil>
     <god>Jaffo</god>
   </gods>'
  */
  // replace all <god> children
  gods.replace('god', <god-substitute>Maho</god-substitute>);
  alert(gods);
  /*
  '<gods>
     <god-substitute>Maho</god-substitute>
     <devil>Xibo</devil>
   </gods>'
  */
  // replace all children
  gods.replace('*', <text>Who is GOD?</text>);
  alert(gods);
  /* shows
  '<gods>
     <text>Who is GOD?</text>
   </gods>'
  */