Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created May 26, 2011 16:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jed/993452 to your computer and use it in GitHub Desktop.
Save jed/993452 to your computer and use it in GitHub Desktop.
write html/text to any node
function(
a, // dom node: only document, element, text, comment, or attribute supported
b, // text or html to write
c
){
c = a.nodeType; // get the node type
c > 8 // if it's the document
? a.close( // close the document after
a.open(), // opening it and
a.write(b) // writing the html,
)
: a[ // otherwise, on the node
[ // get the appropriate property,
, // (elided placeholder for 0)
"innerHTML", // "innerHTML" for nodeType==1 (elements),
"value" // "value" for nodeType==2 (attributes),
][c] // based on the nodeType,
|| "nodeValue" // or use "nodeValue" (comments and text nodes),
] = b // and set the text or html.
}
function(a,b,c){c=a.nodeType;c>8?a.close(a.open(),a.write(b)):a[[,"innerHTML","value"][c]||"nodeValue"]=b}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "write",
"description": "write html/text to any node",
"keywords": [
"innerHTML",
"write",
"attribute",
"DOM"
]
}
@arextar
Copy link

arextar commented Sep 27, 2011

Consider adding:

c == 1 && "value" in a ? c = 2 : 0 //If the element has the 'value' property, force it into an attribute type

to support setting the value of an input element?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment