jlg1987 jlg1987 - 1 year ago 81
Javascript Question

Raphael setting the attr of an element

I am using raphaeljs add need to set attributes on an element, however the attributes name and value are saved in variables

var attr_name='fill';
var attr_value='#343434';

but the following does not work

exampleelement.attr({attr_name: attr_value});

however if I type in the actual name in the attr field it works

exampleelement.attr({'fill': attr_value});

I have tried wrapping it in quotes and double quotes but this has no effect.

Please can anyone suggest anything?
It is not really an option to manually type them in as this code runs in a loop and each time it could be a different attribute it is setting.

Answer Source

Try this -

exampleelement.attr(attr_name, attr_value);

jQuery Docs - .attr()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download