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

The Archive Base Latest Questions

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

I am creating a jquery function which allows users to create rules and append

  • 0

I am creating a jquery function which allows users to create rules and append these rules to a list. I am currently writing the list item html in long form inside the function and it works exactly how it is meant to, but I was wondering if there was a better/more efficient way to create this new list item than writing out all of the html?

Here is the code I currently use:

var li = jQuery('<li>').html('<div id\="rule-desc\">' + desc + '</div>'
                        +'<input type=\"button\" class=\"remove-rule\" value=\"Remove\" />'
                    + '<input type=\"hidden\" name=\"rule['+ index + '][type]" value="' + rule_type + '"/>'
                        + '<input type=\"hidden\" name=\"rule['+ index + '][value]" value="' + value + '"/>'

jQuery("#element-rules").append(li);

Thanks for looking.

  • 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:41:48+00:00Added an answer on May 20, 2026 at 2:41 am

    This looks like a staple case for something like mustache.js or handlerbars.js. If you were using, say, Handlebars, your code would look like this:

    // Once, compile the template
    var template = '
      <div id="rule-desc">{{desc}}</div>
      <input type="button" class="remove-rule" value="Remove" />
      <input type="hidden" name="rule[{{index}}][type]" value="{{rule_type}}" />
      <input type="hidden" name="rule[{{index}}][value]" value="{{value}}" />';
    
    var $ruleset = Handlebars.compile(template);
    
    // Then, whenever you build your rule:
    jQuery("#element-rules").append(jQuery("<li>").html($ruleset({
      desc: desc,
      index: index,
      value: value,
      rule_type: rule_type
    }));
    

    The basic idea is that you build a template, and then generate HTML by passing a hash of properties to the compiled template, from which you get HTML back. It makes large-scale management of this sort of thing quite easy.

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

Sidebar

Related Questions

I'm reading some tutorials now about jQuery.. function creating,plugin creation etc. but these tutorials
jQuery's extend function has the option of recursive extension, which I use when creating
I have the following JQuery which allows a user to select/de-select a list of
I am creating my own jQuery plugin. Here is the code which I have
I'm creating a jQuery plugin which adds some custom animations to an element and
I'm creating a feature in my software that allows users to edit multiple items
I'm creating a small code plugin which allows you to do some things with
I am creating a Javascript object that contains a function that executes a jQuery
I'm creating namespaced events with jquery. When I use the following function with code=112,
I'm currently creating an jQuery Plugin called Truncate - The Purpose of it is

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.