Write the shortest text getter/setter on an element for the DOM

8

3

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!

Florian Margaine

Posted 2013-04-08T16:59:46.940

Reputation: 286

is function name going to be counted towards the total? – Shmiddty – 2013-04-08T17:07:59.163

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. – Florian Margaine – 2013-04-08T17:09:45.360

1So, one letter function names. Got it. – Shmiddty – 2013-04-08T17:10:38.370

@Shmiddty Also, not having the function declaration would be unfair to people wanting to use recursion. – Florian Margaine – 2013-04-08T20:17:50.217

Answers

4

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

Denys Séguret

Posted 2013-04-08T16:59:46.940

Reputation: 780

if('split' in h) – Shmiddty – 2013-04-08T17:27:44.563

@Shmiddty That would force the function user to pass something not undefined. – Denys Séguret – 2013-04-08T17:31:10.337

The code golf SE counter reports 175 characters, not 218. Why is it so? – John Dvorak – 2013-04-08T17:40:29.567

@JanDvorak where's this counter ? It's my first time here... – Denys Séguret – 2013-04-08T17:42:36.547

@dystroy http://stackapps.com/questions/2048/code-golf-userscript-enhancement-pack

– John Dvorak – 2013-04-08T17:44:13.087

There's a formatting problem : if you edit my answer you see a bigger code... – Denys Séguret – 2013-04-08T17:44:25.430

1Hint that other people can use: data is shorter than nodeValue :-). Can you post the non-golfed version too? – Florian Margaine – 2013-04-08T17:50:08.547

Well... As I have no makefile to golf it, I don't have any non-golfed version anymore. But I'll write it properly with 20 chars variable names as soon as I decide to stop shrinking it. – Denys Séguret – 2013-04-08T17:54:15.270

2

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))}}

Shmiddty

Posted 2013-04-08T16:59:46.940

Reputation: 1 209