2

If I want to create:

<foo>
  <bar>example1</bar>
  <bar>example2</bar>
</foo>

using e4x, and I already have:

foo.bar = <bar>example1</bar>

How do I add the additional bar tag?

SCdF
  • 57,260
  • 24
  • 77
  • 113
  • 1
    I just edited my answer: `foo.bar` should be wrong. `foo += ...` is what we need here. E4X is designed to work just like usual JavaScript objects (especially like `Object` literals). So, there really is no big mystery other than the lack of proper documentation (the IBM site is kind of helpful, though). – FK82 Sep 20 '10 at 10:15
  • Yeah, I'm having to ramp up quickly on this, thought I'd ping SO about it ;-) The actual ecma spec isn't entirely shoddy either, though yeah, documentation is a little thin on the ground. – SCdF Sep 20 '10 at 10:54
  • If you are talking about the *white paper*: it's a good look-up, but lacks examples imo. Anyhow, if you stick to the general javascript pattern with E4X you should do just fine. Good luck! – FK82 Sep 20 '10 at 14:06

1 Answers1

3

You either write another literal,

foo += <bar>example3</bar> ;

or you use the relevant methods: http://www.ibm.com/developerworks/library/x-javascript4x.html (Table-1),

var addition = <bar>example3</bar>
foo.insertChildAfter(foo.bar[1],addition) ;

HTH

FK82
  • 4,907
  • 4
  • 29
  • 42
  • I tested on rhino JS on the JDK and it should be foo.bar += example3 ; It looks like += appends siblings. – Alex Pi Feb 03 '17 at 18:06
  • I believe if you have other nodes besides bar inside foo, += will append at the end not after the bar. So for insertChildAfter will do the right job getting instead of – Pablo Pazos Nov 15 '18 at 16:56