Client Side Things
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesClient Side Things

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 2nd, 2005, 12:50 PM
Ursus Ursus is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Maryland, USA
Posts: 402 Ursus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 4 m 30 sec
Reputation Power: 2
building javascript objects

I'm trying to use js to loop through a set of fields and build an object from the field names and values. Can someone tell me why the code below isn't working? I've stuck in a lot of alerts for debugging purposes, and the alerts are returning the correct values, but the the values of all the properties are being returned as 'undefined'.

Code:
<html>
	<head>
	<script type='text/javascript'>
		function buildObj(setNum)
			{
			var obj=new Object();
			var fldList=new Array('name','job','gender');
			for(var i=0; i<fldList.length; i++)
				{
				var k= fldList[i];
				var fld=k+setNum
				var val=document.getElementById(fld).value;
				alert('Value of ' + fld +'='+val+'nSetting ' +k+'='+val);
				obj[k]=val;
				}
			return obj;
			}

		function getStr(obj)
			{
			var str='Your Object:n';
			for(var key in obj)
				{
				str+=key+":"+obj.key+"n";
				}
			return str
			}
	</script>
	</head>
<body>
	Name: <input type='text' id='name1' /><br />
	Job: <input type='text' id='job1' /><br />
	Gender: <input type='text' id='gender1' /><br /> 
	<button id='btn' onclick='var n=buildObj(1); var s=getStr(n); alert(s);'>Go</button><br />
</body>
</html>


Thanks a ton.

Reply With Quote
  #2  
Old September 2nd, 2005, 12:54 PM
Ursus Ursus is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Maryland, USA
Posts: 402 Ursus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 4 m 30 sec
Reputation Power: 2
RE: building javascript objects

Never mind. I'm an idiot.

The code building the array was working. The problem was in the code printing the array in the alert.

Sorry.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesClient Side Things > building javascript objects


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT