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

  • SEARCH
  • Home
  • 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 3959448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:43:54+00:00 2026-05-20T02:43:54+00:00

I am following the best practices concerning the DOM manipulation topic. I am having

  • 0

I am following the “best practices” concerning the DOM manipulation topic. I am having a problem though. Before, my code was:

for(var i=0;i<size;++i){
    var $li = $('<li/>',{some propertie..}).data(some values);
    $ul.append($li);
}

After reading a bit of performance tips, I concluded that I need to replace this into:

var str_html = '';
for(var i=0;i<size;++i){
    var li  = '<li ...>...</li>';
    str_html += li;
}
$ul.append(str_html);

My question is, how can I add the data parameters in this second approach, the same way I was doing in the first one (for each li element)?
Thanks!

  • 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-05-20T02:43:54+00:00Added an answer on May 20, 2026 at 2:43 am

    Since jQuery 1.4.3 you can set data attributeshave a read. This would look like:

    var str_html = '';
    for(var i=0;i<size;++i){
       var li  = '<li data-somevalue="foobar">...</li>';
       str_html += li;
    }
    $ul.append(str_html);
    

    jQuery will automatically pull all data- attributes into the elements data-expando. So you could access the above example with:

    $('li').data('somevalue') // === foobar
    

    You actually can put in any type of data into such an attribute, even JSON decoded object-string literals (which automatically get parsed by jQuery):

    '<li data-somevalue="{'foo':'Barrrrr'}">...</li>';
    

    is translated into

    $('li').data('somevalue').foo // === Barrrr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to take my code to the next level. Following some best practices
I'm trying to create a jQuery plugin following some official best practices (function($){ var
I am looking for information concerning the following: What are the best practices for
Which of the following code is fastest/best practice for converting some object x? int
I just realized that i may not be following best practices in regards to
What is the best way of transcribing the following Transact-SQL code to Informix Dynamic
I've got a plugin that is declared and hooked following best practices described in
I have read the following which is from Best Practices for Encoding Video with
I am using ASP.NET MVC with ASP.NET membership. Following best practices for 'I forgot
I have a question concerning best-practices for indexing in SQL Server (or any RDBMS

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.