It must suck to be human. If I was born a human (or as you), I would have killed myself.
- Edelweiss, Sagashi-Ateru 2: Prisoners of Fate

Home » Post Item » Removing Attributes in Elements using Javascript

Removing Attributes in Elements using Javascript

Wednesday, December 19, 2007

hey! here's another cool thing that i found…

getting irritated of that nasty "setting a property that has only a getter" error?

that error is caused when you tried to change something that couldn't be changed or add something that could not be added.. some element attributes are read-only and cannot be modified  such as xul's selected attribute in it's radio button implementation

<radio label="Dinner Time" name="options" id="dinner" value="4" disabled="true" selected = true /> 

if you put in your javascript something like:

if (document.getElementById('foobaz') == true) {
    document.getElementById('lunch').selected = false;
}

thinking that selected will be set to false.. well in xul there is no selected = false, it's either selected = true or no selected attribute at all

well what if you wanted to not select it you can use a javascript function called removeAttribute it removes the selected attribute so the radio button is not selected and the value will be nothing..

if (document.getElementById('foobaz') == true) {
    document.getElementById('lunch').removeAttribute('selected');
}

now, what if you want to place it to another? simply use the setAttribute function like so:

if (document.getElementById('foobaz') == true) {
        document.getElementById('lunch').removeAttribute('selected');
        document.getElementById('dinner').setAttribute('selected',true);
        }

it adds an attribute selected and assigns it as true.

now as for the values for the radiogroup maybe you can use the value function.. 

hope that helps ^^, feel free to comment if you have any questions ^^,

Posted by roiji at 11:59 am | permalink

Add a comment