Skip to content

Instantly share code, notes, and snippets.

@keriati
Created August 23, 2012 12:52
Show Gist options
  • Save keriati/3436336 to your computer and use it in GitHub Desktop.
Save keriati/3436336 to your computer and use it in GitHub Desktop.
Change the text content in one element.
/**
* 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