user2641233
user2641233

Reputation: 109

jquery html() always changes content

I made a small cms where the frontend is based on jquery. My problem is that sometimes in my module-template code i use something like this:

 <condition>

    </td></tr><tr>

</condition>

When i grap this template with jquerys html function:

var htmlString = $("#templateid").html();

jquery modifies the content in the condition tag. The ouput is

 <condition>

</condition>

Its just works when the code in the condition tag is complete like

<table><tr><td></td></tr></table>

Is there any function where jquery outputs the html without modifying the content?

Upvotes: 0

Views: 91

Answers (1)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93561

Your HTML snippet is invalid, so is destroyed in a normal element.

If you absolutely have to have a partial snippet use a dummy script block instead, so it remains as text:

<script id="condition" type="text/template">

    </td></tr><tr>

</script>

Access with $('#condition').html() etc.

If you can, refactor your code to use complete elements. Then they can exist within other elements and not just inside script blocks.

Notes:

  • Type text/template is an unrecognized type (I just use that one for my templates) so is just ignored by the browser.

Upvotes: 2

Related Questions