Skip to content Skip to sidebar Skip to footer

Replace Any Plain-text To Class Of Specific Div

Trying to replace any plain-text like [make-this-class] to class like
of specific div
.

Solution 1:

Try utilizing .html() , String.prototype.replace() with RegExp/\[(.*)\]/

$(".this-div-only").html(function(i, html) {
  return html.replace(/\[(.*)\]/, $("<div />", {"class":"$1"})[0].outerHTML)
})
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><divclass='not-others-div'>
text text [make-this-class] text
</div>
Text text text
<divclass='this-div-only'><!-- this div only -->
text text [make-this-class] text
</div>

Solution 2:

you store the content of your div, replace the string you're looking for and finally update your div's content with new content.

var content = $('.this-div-only').html();

var newcontent = content.replace('[make-this-class]', '<div class="make-this-class"></div>');

$('.this-div-only').html(newcontent);

Post a Comment for "Replace Any Plain-text To Class Of Specific Div"