var obj = document.createElement('div');
obj.className = 'className';
This will output the div element with class appended to it.
var obj = {
divE : function(){
return document.createElement('div')}
}
obj.divE().className = 'className';
This doesnot append the class name to the div element i.e. obj.divE()
What could be the reason? How to append class to obj.divE();
The returned value of
obj.divE().className = 'className';statement isclassNamestring. Your code creates an element, modifies it'sclassNameproperty and effectively doesn't do anything. If you store the returned value:then the stored value is
'className'string not the created element. You have to create the element, store it and then update theclassNamejust like your first snippet.If you want to create a helper function you can code: