Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop with innerHTML ? 1

Status
Not open for further replies.

TommyB44

Technical User
Jun 16, 2005
76
GB
Hi, All
I'm trying to loop this code to write a table within a span id
so that the whole page doesn't have to be re-wrote, i don't
want to use and iframe and i don't think i can use document.write
without it wanting to write the whole page over again.

The following code will only write the last person on the list1.js,
actually i think it writes them all, but the loop writes over the
ones before it. Does anyone know how i can get around this ?.
I'm a noobie when it comes to javascript.

The list1.js content is...
var s="#2111~bob~22~Spain#3221~paul~26~USA#5476~sam~45~UK";

The code...

Code:
<script id="pid" src="list1.js">
</script>
<body onload=Getlist();>
<script>
function Getlist(){
var users = s.split("#");
for(i=1;i<users.length;i++){
  user=users[i].split("~");
  users[i]={ 
userID: user[0], userName: user[1], age: user[2], location: user[3]
  } }
for(i=1;i<users.length;i++){ 
list.innerHTML="<tr id="+users[i].userID+"><td>"+users[i].userName+"</td><td>" + users[i].age + users[i].location+"</td>";
}}
</script>
<other html stuff tables and so on>

<SPAN id="list">
</SPAN>

<more html>

Thanks for reading.
 
just eyeballing without testing, but something along these lines:

Code:
<script>
function Getlist() {
	var users = s.split("#");
	for(i=1; i<users.length; i++){
		user = users[i].split("~");
		users[i]={
			userID: user[0],
			userName: user[1],
			age: user[2],
			location: user[3]
		}
	}
	
	var list = document.getElementById("list");
	list.innerHTML = "<table>";
	
	for(i=1; i<users.length; i++){
		list.innerHTML += "<tr id="+users[i].userID+">"
			+ "<td>"+users[i].userName+"</td>"
			+ "<td>" + users[i].age + users[i].location+"</td>";
	}
	
	list.innerHTML += "<table>";
}
</script>

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
no prob...

that last bit should be
list.innerHTML += "<[highlight]/[/highlight]table>";

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top