Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created May 11, 2011 09:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jed/966191 to your computer and use it in GitHub Desktop.
Save jed/966191 to your computer and use it in GitHub Desktop.
swap DOM nodes with HTML
function(
a, // the node to be replaced
b, // the node or HTML to replace it
c, // placeholder
){
a.parentNode // from the parent of the given node,
.replaceChild( b.nodeType // replace the original node with
? b // the replacement node, if it is a node,
: ( // or otherwise
( // create it by
( c = document // getting the document,
.createElement("a") // creating any element,
)
.innerHTML = // and writing its html
b.replace( // by replacing
/^\s+/, // any leading whitespace
"" // with nothing,
)
),
c.firstChild // and then fetching its first child.
),
a
)
}
function(a,b,c){a.parentNode.replaceChild(b.nodeType?b:(((c=document.createElement("a")).innerHTML=b.replace(/^\s+/,"")),c.firstChild),a)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
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": "swap",
"keywords": ["swap", "replace", "DOM", "HTML"]
}
@atk
Copy link

atk commented Jun 9, 2011

"/^\s_(._)/(b)[1]" is 4 chars shorter than "b.replace(/^\s+/,"")", but does the same.

@jed
Copy link
Author

jed commented Jun 9, 2011

@atk, regexps don't execute in IE.

@atk
Copy link

atk commented Jun 9, 2011

f..., I forgot about IE! Sorry - but I found another way to shave off 2 bytes: instead of b.nodeType, you could test ""+b===b.

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