Shane Wade
Shane Wade

Reputation: 99

How to write HTML codes inside JQuery when results getting JSON

I have some HTML contents showing through Json JQuery.

That content has almost 400 lines. when I write the code inside Jquery I have to remove all the white spaces. I mean like this

var code = "<div><a href="">this is test</a><p>This is paragraph</p></div>";

something like that. above code is not a problem. it is a simple code.but when I want to use more codes it is not an easy. so is there have any easy way to write HTML codes inside Jquery?

Upvotes: 0

Views: 97

Answers (3)

Jeff McCloud
Jeff McCloud

Reputation: 5927

To keep things cleaner, you could keep the HTML in a separate file, but load it using jQuery's .load() method:

$( "<div />" ).load( "/your_file.html", function() {
  // TODO: use this.innerHTML to get HTML string, or $(this) as jQuery element
});

Upvotes: 0

RizkiDPrast
RizkiDPrast

Reputation: 1725

I will just guessing what you are asking

//supposed you have like this
var precode = '<div>\n\r<a href="">this is test</a>\n\r<p>This is paragraph</p>\n\r</div>';
document.body.innerHTML = precode;

//and you want to remove spaces like this
var code = precode.replace(/\s{2,}/g, "");
document.body.innerHTML += code;

console.log("precode:", precode);
console.log("code:", code);

Upvotes: 0

kzhao14
kzhao14

Reputation: 2630

You can use the ` character in place of ".

Edit: This is not currently supported in Internet Explorer. Instead, you can use a \ at the end of very line:

var text = "<h1>big</h1> \
<h2>smaller</h2> \
<h3>even smaller</h3> \
<h6>smallest</h6>";
document.write(text);

var text = `<h1>big</h1>
<h2>smaller</h2>
<h3>even smaller</h3>
<h6>smallest</h6>`;
document.write(text);

Upvotes: 1

Related Questions