-3

My HTML like the below (example)

"<i class='la la-angle-double-right'></i>"

Let say I use ajax to load the HTML content and push it into a div (ajaxify concept).

But when I call $(element).html(); it return

"<i class="la la-angle-double-right"></i>"

then make the js face the issue Unexpected identifier at HTMLDivElement... if I call $(element).text(); its will return '' // empty string.

The question is how can I take the original HTML via jQuery in my case?

Note: my full content is a full content of a <script>...</script>.

Nguyễn Top
  • 352
  • 2
  • 5
  • what content ajax request returns? `.html()` should work for you, just check response of your request – demo Feb 22 '19 at 14:42
  • What are you trying to accomplish? First, there is no text inside the element, just elements, so $(element).text() will indeed result in an empty string. $(element).html() (if you're point to the correct element) should return your html as intended. – Leon Feb 22 '19 at 14:42
  • @demo the response from server is exactly `""`, but after wrap `$(content)` (I find it by slector) then call `.html()` it return `""`. – Nguyễn Top Feb 22 '19 at 14:47
  • @Leon its point to correct element, it so weird that after $(element) the HTML char ' become " – Nguyễn Top Feb 22 '19 at 14:49
  • @AswinKumar, the content not only the `i` tag, im just give example - the true content is whole page (Im use ajax to request the page, then use jquery to update the UI to prevent browser re-render whole page - ajaxify concept). – Nguyễn Top Feb 22 '19 at 14:50
  • The only difference I see is the quotes. You can do an easy replace function on the html string to replace the double quotes with single quotes. – Mark Baijens Feb 22 '19 at 15:04
  • @MarkBaijens I tried it, but its not cover all html content. Let say we will have `src=""` `href=""` not only `class="...." and then end by ">` – Nguyễn Top Feb 22 '19 at 15:05
  • See [here](https://stackoverflow.com/a/10610408/2159528) to replace all occurance in a string. – Louys Patrice Bessette Feb 22 '19 at 15:08
  • @freedomn-m: OP uses the returned string to feed some datatables option... See the image in question. – Louys Patrice Bessette Feb 22 '19 at 15:11
  • How are you generating the "oPaginate" script from your screenshot? That appears to be the issue. – freedomn-m Feb 22 '19 at 15:11
  • What I can see from your image i see that your oPaginate contains an object with html strings that are not escaped. The problem is probably in the way you build that object. You will need to escape the double quotes there. How to you fill that? – Mark Baijens Feb 22 '19 at 15:11

1 Answers1

1

Both <i class="la la-angle-double-right"></i> and <i class="la la-angle-double-right"></i> are a valid representations of the same thing.

If you get html and convert it to DOM then the original html formatting will be lost, the browser will not keep the original html text, so there is no way to get that original formatting back from the DOM Api.

The $(element).html(); - or to be more precisely the DOM API of browser - will convert the DOM Element back to html, and uses formatting that the implementers of the browser choose. So it could be <i class="la la-angle-double-right"></i>, <i class='la la-angle-double-right'></i>, <I class="la la-angle-double-right"></I>, <I CLASS="la la-angle-double-right"></I>, ...

You should not expect (or even relay) on a certain format.

You problem is that you don't escape the data properly when you work with it. You have to write:

{
  "firstName": "<i class=\"la la-angle-double-right\"></i>"
  /* ... */
}
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • Yeah, you are right and I understand it, but my stage is migrate the old system to new one, that why I prevent to escape the code one by one. Just try to make sure there is no way, thank for your answer. – Nguyễn Top Feb 22 '19 at 15:13