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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:52:56+00:00 2026-05-22T23:52:56+00:00

I have asked a question about how to avoiding to write the html in

  • 0

I have asked a question about how to avoiding to write the html in the js,then some people tell me using the javascript template,for example,the jquery/template pugin and ect.

It is a good idea when generate static html,for example:

<ul id="productList"></ul>

<script id="productTemplate" type="text/x-jquery-tmpl">
    <li><a>${Name}</a> (${Price})</li>
</script>

<script type="text/javascript">
    var products = [
        { Name: "xxx", Price: "xxx" },
        { Name: "yyy", Price: "xxx" },
        { Name: "zzz", Price: "xxx" }
    ];

    // Render the template with the products data and insert
    // the rendered HTML under the "productList" element
    $( "#productTemplate" ).tmpl( products )
        .appendTo( "#productList" );
</script>

However when I try to bind some event to the generated html,I meet some problem.

For example,I have a page which user can search some products by the price/name/location.

So I have three function:

searchByPrice(lowPrice,highPrice,productType,currentPage)
searchByName(name,productType,currentPage);
searchByLocation(location,currentpage);

ALl the above function have a realated method in the server side and they will retrun the products usint the xml format.

Since they will retrun so many items,so I have to paging them,the “currengPage” is used to tell the server side which part of results should be returned.

When the client get the result from the server side,now it is the js for display them int he div and create a Paging Bar if possible.

Before I know the template,I use this manner(which I hate most,try my best to avoid):

function searchByPrice(lowPrice,highPrice,productType,currentPage){
    var url="WebService.asmx/searchByPrice?low="+lowPrice="&high="+highPrice+"&curPage="+currentPage;
    //code to create the xmlHttp object
    xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            var i=0;
            var Prohtml="";
            var proList=parseProductList(xmlhttp.responseText);
            for(i=0;i<prolist.length;i++){
                Prohtml+="<li><a href='#'>"+prolist[i].name+"</a> ("+prolist[i].price"+)</li>";
            }


            //generate the paging bar:
            var totleResult=getTotleResultNumber(xmlhttp.responseText);
            if(totleResult>10){
                var paghtml="<span>";
                //need the paging
                var pagNum=totleResult/10+1;
                for(i=1;i<=pagenum;i++){
                    paghtml+="<a onclick='searchByPrice(lowPrice,highPrice,productType,currentPage+1)'>i</a>";
                    //here the synax is not right,since I am really not good at handle the single or doule '"' in this manner.

                    //also if in the searchByName function,the click function here should be replaced using the searchByName(...)


                }

            }
        }
    }

}

In the example,it is easy to use the template to generate the “Prohtml” since there is no event handling with them,but how about the “paghtml”,the click function is different in differnt search type.

So,any good idea to hanld this?

  • 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-22T23:52:57+00:00Added an answer on May 22, 2026 at 11:52 pm

    Either:

    Create DOM Elements instead of building HTML strings, using document.createElement or a small library if you’re doing lots of this, which will allow you to attach events immediately in the usual fashion.

    or

    Give each element which needs to make use of event handlers a unique ID and build up a list of events to be attached once the HTML has been inserted into the document.

    E.g.:

    var eventHandlers = []
      , eventCount = 0;
    
    for (i = 1; i <= pagenum; i++) {
        var id = "search" + eventCount++;
        html += "<a id='" + id + "'>" + i + "</a>";
        eventHandlers.push([id, 'click',
                            handler(searchByPrice, lowPrice, highPrice, productType, currentPage + i)])
    }
    
    // Later...
    someElement.innerHTML = html;
    registerEvents(eventHandlers);
    

    Where registerEvents is:

    function registerEvents(eventHandlers) {
      for (var i = 0, l = eventHandlers.length; i < l; i++) {
        var eventHandler = eventHandlers[i],
            id = eventHandler[0],
            eventName = eventHandler[1],
            func = eventHandler[2];
        // Where addEvent is your cross-browser event registration function
        // of choice...
        addEvent(document.getElementById(id), eventName, func);
      }
    }
    

    And handler is just a quick way to close over all the arguments passed in:

    /**
     * Creates a fnction which calls the given function with any additional
     * arguments passed in.
     */
    function handler(func) {
      var args = Array.prototype.slice.call(arguments, 1);
      return function() {
        func.apply(this, args);
      }
    }
    

    I use something like this approach (but automatically adding unique ids when necessary) in the HTML generation portion of my DOMBuilder library, which offers a convenience method for generating HTML from content you’ve defined, inserting it into a given element with innerHTML and registering any event handlers which were present. Its syntax for defining content is independent of output mode, which allows you to switch between DOM and HTML output seamlessly in most cases.

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

Sidebar

Related Questions

I have asked this question about using the a Linq method that returns one
Recently I have asked a question about what I should use to create self-contained
I have asked this question here about a Python command that fetches a URL
I have already asked a question about this, but the problems keeps on hitting
I have asked a few question lately about the use of the singleton and
I have asked a question on SuperUser about updating Ruby version in Google SketchUp
I have asked a similar question before, but I didn't have a firm grasp
This is a continuation question from a previous question I have asked I now
This question has been asked before ( link ) but I have slightly different
Question: I have a question that is apparently not answered by this already-asked Bash

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.