-
-
Save jed/966191 to your computer and use it in GitHub Desktop.
swap DOM nodes with HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b,c){a.parentNode.replaceChild(b.nodeType?b:(((c=document.createElement("a")).innerHTML=b.replace(/^\s+/,"")),c.firstChild),a)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "swap", | |
"keywords": ["swap", "replace", "DOM", "HTML"] | |
} |
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
@atk, regexps don't execute in IE.