Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2012
    Posts
    137
    Thanks
    1
    Thanked 1 Time in 1 Post

    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>

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    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>

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •