Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:22:58+00:00 2026-06-16T05:22:58+00:00

I am trying to append li elements coming from a php file with JQuery.

  • 0

I am trying to append li 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 append() can only append correct html with all elements closed. Otherwise it will automatically close the tags. The following code will NOT work as dval contains code like <div><li class="some">Some value</li> and append() will make <div><li class="some">Some value</li></div> out of it. So I was wondering whether there is another way, maybe a function other than append() to be able to append html parts?

EDIT:

This is my Jquery:

$.getJSON("../ajax.php", function(data)
{
    $.each(obj, function(key,val)
    {
        $.each(obj[key], function(key, dval)
        {                       
            if(key == "text")
            {                   
                $("#" + key).append(dval);
            }           
        })          
    });
});

This is my PHP:

$json_arr = $json_arr."[";
if($counter % 2 == 0)
{
    $arr[0] = preg_replace('#<td class="some">(.*?)</td>#', '<li id="1234"><span class="some">$1</span>', $ja[0]);
}
else $ja[0] = preg_replace('#<td class="some">(.*?)</td>#', '<span class="some2">$1</span><div class="somediv"></div></li>', $ja[0]);

$json->id = $closest_key; // Closest key is the ID of an existing li element which is the closest to the current word
$json->text = $ja[0];
$json_arr = $json_arr.'{"id":"'.$json->id.'", "text":"'.addslashes($json->text).'"},';
$json_arr = substr($json_arr,0,-1);
$json_arr = $json_arr."]";
echo(json_encode($json_arr));

Now if counter is even <li id="1234"><span class="some">$1</span> should be appended to $closest_key, if it is odd then <span class="some2">$1</span><div class="somediv"></div></li> should be appended.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:22:59+00:00Added an answer on June 16, 2026 at 5:22 am

    Its not realy jquery that is adding the closing tag, its your browser. In order to render a webpage it needs to have a valid HTML DOM and it will try to fix errors for you.

    What you are doing will be very slow with a large obj, because after every append the entire page will be recreated (meaning every width/height/position must be recalculated). (google javascript reflow / repaint)

    The best thing to do is to create the full HTML strings first and then append it.

    var html = {};
    $.each(obj, function(key,val)
    {
        $.each(obj[key], function(key, dval)
        {                       
            if(key == "text")
            {            
                if (html[key]==undefined)
                  html[key] = "";
    
                html[key] += dval.toString();
            }           
        })          
    }); 
    
    $.each(html, function(key,val)
    {
        $("#" + key).append(val);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use append to generate html using data from an xml file.
I am trying to append to a log file, using HTML tables format for
I'm trying to remove all img tags from the DOM and the elements that
I'm trying to dynamically add elements to a Cakephp form: <?php echo $javascript->link('jquery',false); ?>
I have an XML that I need remove empty elements from. I am trying
Here is the code I have, I am trying to append the elements in
I am trying to generate a simple jQuery code snippet in JavaScript and append
I'm trying to append a string of svg elements to the dom. This is
I am trying to create a new Element in my javascript code and append
I'm trying to append a transparent layer to some elements with an edit button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.