CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   Should I do this with HTML or jQuery loop (http://www.codingforums.com/showthread.php?t=287321)

m2244 02-08-2013 03:45 PM

Should I do this with HTML or jQuery loop
 
Hello,

Right now I have some HTML code on my page that will be used for a glossary feature. I am wondering if I should create this list with jQuery or leave it as HTML.

My concerns are loading time as well as code simplification.

Code:

<div>
        <a href="#">A</a>
        <a href="#">B</a>
        <a href="#">C</a>
        <a href="#">D</a>
        <a href="#">E</a>
        <a href="#">F</a>
        <a href="#">G</a>
        <a href="#">H</a>
        <a href="#">I</a>
        <a href="#">J</a>
        <a href="#">K</a>
        <a href="#">L</a>
        <a href="#">M</a>
        <a href="#">N</a>
        <a href="#">O</a>
        <a href="#">P</a>
        <a href="#">Q</a>
        <a href="#">R</a>
        <a href="#">S</a>
        <a href="#">T</a>
        <a href="#">U</a>
        <a href="#">V</a>
        <a href="#">W</a>
        <a href="#">X</a>
        <a href="#">Y</a>
        <a href="#">Z</a>
</div>


niralsoni 02-08-2013 04:54 PM

Well, as of now it would look fine, but when you are actually adding the items to it, you may face some problems.

Following snippet might help you in achieving same using JavaScript (you can replace a bit of code with jQuery)

Code:

<div id="glossary"></div>

<script type="text/javascript">
  function loadGlossary(id) {
    var obj = document.getElementById(id), temp;
    for(var i = 65; i <=90; i++) {
      temp = document.createElement('A');
      temp.innerHTML = '&#'+i;
      temp.href = '#';
      obj.appendChild(temp);
      temp = document.createTextNode(' | ');
      obj.appendChild(temp);
    }
  }
  loadGlossary('glossary');
</script>


felgall 02-08-2013 08:04 PM

One thing to remember is that not everyone has JavaScript. JavaScript should only be used to add features to a web page that make it easier to use - not to provide the basic functioning that the page is there for.

If you want to generate that code instead of having to have it all hard coded then generate it using a server side language. That way you have the benefit of it being program generated AND everyone including those without JavaScript still sees the same HTML.


All times are GMT +1. The time now is 02:39 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.