8
\$\begingroup\$

Just like jQuery's $.fn.text, write one or two functions that can get and set the text on an element.

You can't use textContent or innerText, and even less innerHTML.

Plain javascript. No DOM library. It has to work on major browsers. (No need to care about old IE.)

Shortest code wins.

Enjoy!

\$\endgroup\$
4
  • \$\begingroup\$ is function name going to be counted towards the total? \$\endgroup\$
    – Shmiddty
    Apr 8 '13 at 17:07
  • \$\begingroup\$ Yep. I should be able to put your function in my page and use it right away. But it should be the same limitation for everyone, so it's not like it's unfair to anyone. \$\endgroup\$ Apr 8 '13 at 17:09
  • 1
    \$\begingroup\$ So, one letter function names. Got it. \$\endgroup\$
    – Shmiddty
    Apr 8 '13 at 17:10
  • \$\begingroup\$ @Shmiddty Also, not having the function declaration would be unfair to people wanting to use recursion. \$\endgroup\$ Apr 8 '13 at 20:17
4
\$\begingroup\$

JavaScript (167)

function t(e,h,f){if(h||h==''){while(f=e.lastChild)e.removeChild(f);e.appendChild(document.createTextNode(h))}h=e.data||'';for(c in f=e.childNodes)h+=t(f[c]);return h}

Usage :

var textContent = t(yourElement); // get the text content
t(yourElement, ''); // clear
t(yourElement, newText); // sets a new text

Note that there is no pollution of the global namespace

Demonstration, with a multi-level DOM tree and a few tests

\$\endgroup\$
8
  • \$\begingroup\$ if('split' in h) \$\endgroup\$
    – Shmiddty
    Apr 8 '13 at 17:27
  • \$\begingroup\$ @Shmiddty That would force the function user to pass something not undefined. \$\endgroup\$ Apr 8 '13 at 17:31
  • \$\begingroup\$ The code golf SE counter reports 175 characters, not 218. Why is it so? \$\endgroup\$ Apr 8 '13 at 17:40
  • \$\begingroup\$ @JanDvorak where's this counter ? It's my first time here... \$\endgroup\$ Apr 8 '13 at 17:42
  • 1
    \$\begingroup\$ Hint that other people can use: data is shorter than nodeValue :-). Can you post the non-golfed version too? \$\endgroup\$ Apr 8 '13 at 17:50
2
\$\begingroup\$

JavaScript (175)

function t(e,v,f){if(!v&&v!=''){v=e.data||'';for(c in f=e.childNodes)v+=t(f[c]);return v}else{ while(c=e.lastChild)e.removeChild(c);e.appendChild(document.createTextNode(v))}}

I have significantly less morality than @dystroy, so I didn't bother with var. I did have to scope f though, and I did it somewhat cleverly.

That said, our answers are pretty similar, and I'm not sure there's much of a better way to go about it.

With collection recognition (173):

function t(e,v){if(!v&&v!=''){v=e.data||'';for each(c in e.childNodes)v+=t(c);return v}else{ while(c=e.lastChild)e.removeChild(c);e.appendChild(document.createTextNode(v))}}
\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 143 bytes

t=(e,h)=>h?.sub?[...e.children].map(c=>e.removeChild(c))&&e.appendChild(document.createTextNode(h)):(e.data||'')+[...e.childNodes].map(c=>t(c))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.