Javascript String Prototype Format to HTML Tag

Description

Javascript String Prototype Format to HTML Tag


// This utility function will wrap any HTML tag around 
// a string literal along with optional CSS class(es)
String.prototype.tagify = function(tag, cssClasses) {
  // First check to see if cssClasses was passed
  if (!cssClasses)
    cssClasses = "";
  // Now ensure we passed a valid tag
  if (tag)/*from  ww  w .java  2s.c  om*/
    return '<' + tag + ' CLASS="' + cssClasses + '">' + this + '</' + tag + '>';
  else
    return this;
}


console.log("a styled DIV".tagify("div", "bigText"));
console.log("a styled SPAN".tagify("span", "bigText"));
console.log("a simple styled paragraph".tagify("p"));



PreviousNext

Related