Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does my JQuery script add html tags when appending it?
    primarykey
    data
    text
    <p>I have a problem when sending a json array to my jquery script where I want to append it to a div. In php I created a json array containing the following html code:</p> <pre><code>&lt;li id="1234"&gt; &lt;span class="some"&gt;$1&lt;/span&gt; &lt;span class="some2"&gt;$1&lt;/span&gt; &lt;div class="somediv"&gt;&lt;/div&gt; &lt;/li&gt; </code></pre> <p>So the whole json array looks like this:</p> <pre><code>[{"id":"12435","text":"&lt;li id=\"1234\"&gt;&lt;span class=\"some\"&gt;$1&lt;/span&gt;"},{"id":"13542","text":"&lt;span class=\"some2\"&gt;$1&lt;/span&gt;&lt;div class=\"somediv\"&gt;&lt;/div&gt;&lt;/li&gt;"}]; </code></pre> <p>Now I used getJSON to get the json object and then I append the result to a div like this: <code>$(".container").append(val);</code></p> <p>Now the problem is that somehow when trying to append the JSON results, this is what is beeing appended:</p> <pre><code>&lt;li id="1234"&gt; &lt;span class="some"&gt;$1&lt;/span&gt; &lt;/li&gt; &lt;li id="1234"&gt; &lt;span class="some2"&gt;$1&lt;/span&gt; &lt;div class="somediv"&gt;&lt;/div&gt; &lt;/li&gt; </code></pre> <p>When I try to output my <code>val</code> with <code>alert(val)</code> it clearly shows that the json result is correct (without the closing <code>&lt;li&gt;</code> tag), but when appending it, the <code>&lt;li&gt;</code> tag is added.</p> <p>Does anyone have any idea what could be the reason for this?</p> <p>This is how I get the values from my php file:</p> <pre><code>$.getJSON("../ajax.php?action=getresults", function(data) { var obj = $.parseJSON(data); $.each(obj, function(key,val) { $.each(obj[key], function(key, dval) { if(key == "text") { alert(dval); $(".container").append(dval); } }) }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload