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 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts

    A table from a DIV contents

    Hi,

    How I build a table based on a list under a <DIV></DIV> like this?

    printing: 1
    and: 3
    typesetting: 2
    industry: 2
    has: 2
    been: 1
    s: 1
    standard: 1
    ever: 1
    since: 1
    1500s: 1
    when: 1
    an: 1
    unknown: 1
    printer: 1
    took: 1
    a: 2
    galley: 1
    type: 3
    scrambled: 1

    Regards

    Bob

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Question Need more information and clarification...

    Can you show what the table should look like from the list you provided?
    I'm not sure I understand how the words relate to the numbers.

    This would be strickly SWAG!
    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    //<![CDATA[
    function createTable() {
     var strList = "printing: 1,and: 3,typesetting: 2,industry: 2,has: 2,been: 1,s: 1,standard: 1,ever: 1,since: 1";
         strList+= ",1500s: 1,when: 1,an: 1,unknown: 1,printer: 1,took: 1,a: 2,galley: 1,type: 3,scrambled: 1";
     var tbl = '<table border="1">';
     var tarr = strList.split(',');
     var tmp = [];
         var r1 = '';
         var r2 = '';
     for (var i=0; i<tarr.length; i++) {
       tmp = tarr[i].split(':');
       r1 += '<th>'+tmp[0]+'</th>';
       r2 += '<td align="right">'+tmp[1]+'</td>';
     }
         tbl += '<tr>'+r1+'</tr>';
         tbl += '<tr>'+r2+'</tr>';
         tbl += '</table>';
       document.getElementById('tblDisplay').innerHTML = tbl;
    }
    //]]>
    </script>
    </head>
    <body onload="createTable()">
    <div id="tblDisplay"></div>
    
    </body>
    </html>
    Last edited by jmrker; 01-09-2012 at 08:15 PM.

  • Users who have thanked jmrker for this post:

    BOBKUSPE (01-09-2012)

  • #3
    New Coder
    Join Date
    Dec 2008
    Posts
    71
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Many thanks.

    Really, my question is based on this functionwith 2 variables and are from these variables I want to create a table:

    Code:
       function countWords(s) {
    
                    if (!window.wordCache) window.wordCache = [];
    
                    var arWords = s.match(/[\w]+/gi);
    
    
                    for (var x = 0; x < arWords.length; x++) {
    
                        countWord(arWords[x]); 
    			
    
                    }
    Regards

    Bob

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb

    You never did indicate where the words are coming from.
    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> Word Counter </title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?p=1179064#post1179064
    
    //<![CDATA[
    
    var wordCache = [];
    function countWords() {
      var s = document.getElementById('TArea').value;
      s = s.replace(/[\n|\r]/g,'');
      wordCache = [];
      var arWords = s.match(/[\w]+/gi);
      for (var x = 0; x < arWords.length; x++) {
        arWords[x] = arWords[x].toLowerCase();
        if (!wordCache[arWords[x]]) { wordCache[arWords[x]] = 0; }
        wordCache[arWords[x]]++;
      }
    }
    
    function createTable() {
     countWords();
     var tbl = '<table border="0">';
     var r1 = '';
     for (var i in wordCache) {
       r1 += '<tr><th width="10">'+i+'</th>';
       r1 += '<td align="right" width="5">'+wordCache[i]+'</td></tr>';
     }
     tbl += r1
     tbl += '</table>';
     document.getElementById('tblDisplay').innerHTML = tbl;
    }
    
    //]]>
    </script>
    </head>
    
    <body>
    <textarea id="TArea" cols="40" rows="5">
    Now is the time for all good men to come to the aid of their country 
    Whatever the mind of man can conceive and believe, he can achieve.
    All looks yellow to a jaundice eye
    </textarea>
    <button onclick="createTable()">Create Table</button>
    <div id="tblDisplay"></div>
    </body>
    </html>
    Last edited by jmrker; 01-10-2012 at 01:33 AM.


  •  

    Posting Permissions

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