angularjs - How to calculate width of text using javascript? -


how width of text.

var e=document.createelement('span');           e.style.fontsize = scope.fontsize;           e.innerhtml = "test";           console.log(e.offsetwidth); 

width comes 0

it seems have append created element document.

var e=document.createelement('span'); document.body.appendchild(e); e.style.fontsize = 14; e.innerhtml = "test"; console.log(e.offsetwidth); e.remove(); 

output: 23


hiding element not work ether.

 e.style.display= "none"; 

output: 0


to "hide it" add css that. added remove() not show anyway. adding position:absolute; idea, since prevent flickering of rest of html content.

   position:absolute;    margin-left:-1000em; 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -