Skip to main content
Inspiring
October 6, 2010
Answered

[CS5 - JS] Setting XML attributes from variables...

  • October 6, 2010
  • 1 reply
  • 1045 views

Hi,

I build an XML variable in order to store informations I need to run my script.

The XML structure is quite simple:

var document = new XML("<document><pages><page/></pages></document>");

All "page" nodes are appended using a simple for statement.

If I need to append an attribute to a "page" node I usually use this method:

document.pages.page.@height= 210;

What if I need to append an attribute without knowing its name (because it is stored in a variable)?

Is there a something like this?:

document.pages.page.setAttribute(var_name, 210);

Have no idea how to handle this...

This topic has been closed for replies.
Correct answer Marc Autret

Try this:

document.pages.page['@'+var_name] = 210;

@+

Marc

1 reply

Marc Autret
Marc AutretCorrect answer
Legend
October 6, 2010

Try this:

document.pages.page['@'+var_name] = 210;

@+

Marc

Inspiring
October 7, 2010

Very helpful, thanks Marc!

PS: It works only if you assign a value to the attribute.