Created
August 23, 2012 12:52
-
-
Save keriati/3436336 to your computer and use it in GitHub Desktop.
Change the text content in one element.
This file contains 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
/** | |
* Change only the text content of some element | |
* | |
* @method changeText | |
* @param element {Object} Element | |
* @param text {String} New text | |
* @param prepend {Boolean} If set to true, the text will be prepended to the element | |
* @return {Boolean} True if the change was successfull, false if not | |
*/ | |
function changeText(element, text, position) { | |
try { | |
var $elem = element; | |
element | |
.contents() | |
.filter(function() { | |
return this.nodeType === 3 | |
}) | |
.remove(); | |
if(position === true) { | |
$elem.prepend(text); | |
} else { | |
$elem.append(text); | |
} | |
return true; | |
} catch (e) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment