Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to append html code in parts via JQuery?
    primarykey
    data
    text
    <p>I am trying to append <code>li</code> elements coming from a php file with JQuery. Problem is that the html code needs to be seperately appended to different html IDs according to the key value. Unfortunately as I understood <code>append()</code> can only append correct html with all elements closed. Otherwise it will automatically close the tags. The following code will NOT work as <code>dval</code> contains code like <code>&lt;div&gt;&lt;li class="some"&gt;Some value&lt;/li&gt;</code> and <code>append()</code> will make <code>&lt;div&gt;&lt;li class="some"&gt;Some value&lt;/li&gt;&lt;/div&gt;</code> out of it. So I was wondering whether there is another way, maybe a function other than <code>append()</code> to be able to append html parts?</p> <p>EDIT:</p> <p>This is my Jquery:</p> <pre><code>$.getJSON("../ajax.php", function(data) { $.each(obj, function(key,val) { $.each(obj[key], function(key, dval) { if(key == "text") { $("#" + key).append(dval); } }) }); }); </code></pre> <p>This is my PHP:</p> <pre><code>$json_arr = $json_arr."["; if($counter % 2 == 0) { $arr[0] = preg_replace('#&lt;td class="some"&gt;(.*?)&lt;/td&gt;#', '&lt;li id="1234"&gt;&lt;span class="some"&gt;$1&lt;/span&gt;', $ja[0]); } else $ja[0] = preg_replace('#&lt;td class="some"&gt;(.*?)&lt;/td&gt;#', '&lt;span class="some2"&gt;$1&lt;/span&gt;&lt;div class="somediv"&gt;&lt;/div&gt;&lt;/li&gt;', $ja[0]); $json-&gt;id = $closest_key; // Closest key is the ID of an existing li element which is the closest to the current word $json-&gt;text = $ja[0]; $json_arr = $json_arr.'{"id":"'.$json-&gt;id.'", "text":"'.addslashes($json-&gt;text).'"},'; $json_arr = substr($json_arr,0,-1); $json_arr = $json_arr."]"; echo(json_encode($json_arr)); </code></pre> <p>Now if counter is even <code>&lt;li id="1234"&gt;&lt;span class="some"&gt;$1&lt;/span&gt;</code> should be appended to <code>$closest_key</code>, if it is odd then <code>&lt;span class="some2"&gt;$1&lt;/span&gt;&lt;div class="somediv"&gt;&lt;/div&gt;&lt;/li&gt;</code> should be appended.</p>
    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.
    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