...

View Full Version : A table from a DIV contents



BOBKUSPE
01-09-2012, 06:19 PM
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

jmrker
01-09-2012, 08:02 PM
Can you show what the table should look like from the list you provided? :confused:
I'm not sure I understand how the words relate to the numbers. :eek:

This would be strickly SWAG!


<!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>

BOBKUSPE
01-09-2012, 09:29 PM
Many thanks.

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



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

jmrker
01-10-2012, 01:30 AM
You never did indicate where the words are coming from.


<!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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum