Reputation: 2187
how cand i add a value to the class element in an image?
for src and align it works, and it adds the values to src and align:
src : this.inputs['src'].value,
align : this.inputs['align'].value,
but when i write class it becomes highlighted and it dosen't add the value to the class element in the images html:
class: this.inputs['class'].value,
is 'class' a reserved word in javascript? if yes is there another solution for my prolem?
Upvotes: 0
Views: 52
Reputation: 75317
class
is a reserved word.
You can still use reserved words as member names in objects by quoting them:
{
'class': 'foo'
}
Regardless, the class
attribute is actually set via className
in JavaScript:
{
src: 'foo.jpg',
align: 'left',
className: 'something'
}
Bear in mind that setting this will overwrite any existing classes you have defined: to combat this you can simply append the new class to the existing value:
this.className += ' another class';
To replace an existing class you can do:
this.className = (" " + this.className + " ").replace(' yourOldClass ',' yourNewClass ');
Upvotes: 1