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 8211013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:16:53+00:00 2026-06-07T10:16:53+00:00

I’m creating an app using PhoneGap and JQuery Mobile where I expect to be

  • 0

I’m creating an app using PhoneGap and JQuery Mobile where I expect to be reading data from some backend service and adding/removing list items (potentially divs as well) based on what is received. Naturally, I want to prevent the app from slowing down too much with prolonged use.

Are there any general best practices when it comes to adding/removing HTML? I’m pretty new to this, so I don’t really understand how the DOM works and whatnot.

Currently I am using JQuery’s append to add lines of HTML to some container div, and using .html(“”) to remove an element. Is this acceptable, or is there a better way of doing this?

Edit: For your reference, I am using JQuery Mobile 1.1.0 and Phonegap 1.9.0. Don’t know if that’s helpful or not, but there you go.

I’m hoping to use this app across as many mobile devices as possible, but I guess iOS and Android are the biggest market right now.

To clarify, I haven’t actually run into major issues (yet), I just wanted to avoid running into anything later on and having to go through a large amount of code to try to fix the problem. I figured it’d be useful for everyone if we could agree on a set of best practices, ie using innerHTML() over .append, remove vs innerHTML(“”), et cetra 😛

Some example code:

function changeList(sel){
            var xmlfolder="/"
            var name = sel[sel.selectedIndex].value+".xml";                
            var location= xmlfolder+name;
            var list = document.getElementById("opList"); //opList is the container
            list.innerHTML=""; //what i'm using to remove the previous occupant
                               //: is there a better way of doing this?
            //reading stuff                
            $.ajax({

                type:'GET',
                cache: false,
                url: location,
                dataType:"xml",
                success: function(data){
                    xmlInfo=data;


                    var i=0;
                    $(data).find("data").each(function(){

                        var string = "";
                        var sName=$(this).find("name").text();

                        string +=("<li>");
                        string +=("<a href=#details data-transition = slide >");
                        string+= sName;
                        string+="</a></li>";
                         $('#opList').append(string); //is there a better way of appending?
                         i++;

                    })

                    $('#opList').listview('refresh');

                },                    
                error:function(e){
                    alert("failed D:");
                    console.log(e);
                }

            });

        }
  • 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-07T10:16:54+00:00Added an answer on June 7, 2026 at 10:16 am

    I’m just going to throw a smattering of ideas at you and we’ll see what sticks!

    Make sure to concoct your HTML and add it to the DOM all at once rather than adding bits and pieces in a loop or something.

    For Example (DO THIS):

    var arr    = ['Title 1', 'Title 2', 'Title 3'],
        output = [];
    
    for (var i = 0, len = arr.length; i < len; i++) {
        output.push('<li>' + arr[i] + '</li>');
    }
    $('ul').append(output.join(''));
    

    Notice how I only select the container once, this is key, even if you do have to reference a DOM element inside a loop, make sure to cache a reference to the element outside the loop, otherwise your’re needlessly doing extra work every iteration.

    And for example (DON’T DO THIS, except for the DOM element caching, that’s good):

    var arr = ['Title 1', 'Title 2', 'Title 3'],
        $ul = $('ul');
    
    for (var i = 0, len = arr.length; i < len; i++) {
        $ul.append('<li>' + arr[i] + '</li>');
    }
    

    If you are going to add some complex HTML structure to the DOM, do the same thing as before, create a “temporary” container, do work on it, then append the whole thing to the DOM at once. For example:

    var $tmpObj = $('<div id="my-div" />').append(
        $('<span class="my-title" />').text('Title 1')
    ).append(
        $('<div class="my-description" />').append(
            $('<span class="my-copy" />').text('Blah Blah Blah')
        )
    ).on('click', function () {
        alert('Don\'t Touch Me!');
    }).data('my-data-key', 'my-data-val');
    
    $('#my-div').replaceWith($tmpObj);
    

    I like to store data associated with an object using $.data() so that when an element is removed, so is it’s data and therefore we aren’t using-up memory with useless data.

    Use JSPerf.com, it’s an amazing tool when used properly. You can run tests to determine what method is faster for an operation. Here is an example one I setup a while ago: http://jsperf.com/jquery-each-vs-for-loops/2

    Don’t needlessly create global variables. When you do this it creates extra overhead when those variables are looked-up. This line: xmlInfo=data; should be var xmlInfo=data; unless xmlInfo is to be accessed outside of the AJAX callback function. Basically when you look-up a variable that is in the global scope while inside of a function (or nested functions); first the browser will look in the current scope, then the parent scope of the current one, all the way up to the global scope. So if you attempt to access a global variable inside of a function inside of a function, you’re now doing about three times as much look-up as if you used a locally scoped variable. If you must use a globally scoped variable, cache it inside of your function and use the local version in your function to avoid the extra loop-up time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
For some reason, after submitting a string like this Jack’s Spindle from a text
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.