Skip to content Skip to sidebar Skip to footer

Convert Html String To Javascript Text Keeping Indentation

I want to target or match only the first occurrence per line Typical Scenario: I have an HTML Structure that I am using in JavaScript.
  • ABC

Solution 1:

I'm not a KomodoEdit user, but I tried these replacements and they worked:

  • starting quote: replace ^(\s*)< with \1<
  • ending quote: replace >\s*$ with >' +

Hope this helps.

Solution 2:

Rather than converting HTML to a JS string, it would be much better to create the elements in JS and put them in the DOM. This would give you much more control, not create such a difficult to maintain/read code, and be much faster (amongst other benefits):

var outerDiv = document.createElement("div");
outerDiv.className = "spa-shell-head";

var innerDivLogo = document.createElement("div");
innerDivLogo.className = "spa-shell-head-logo";

var innerDivAcct = document.createElement("div");
innerDivAcct.className = "spa-shell-head-acct";

var innerDivSearch = document.createElement("div");
innerDivSearch.className = "spa-shell-head-search";

outerDiv.appendChild(innerDivLogo);
outerDiv.appendChild(innerDivAcct);
outerDiv.appendChild(innerDivSearch);
document.body.appendChild(outerDiv);

The above creates the following:

enter image description here

https://jsfiddle.net/yfeLbhe4/

Post a Comment for "Convert Html String To Javascript Text Keeping Indentation"