Jump to content

Strange output of toSource()


brucemand
 Share

Recommended Posts

i'd been banging my head for quite a while and finally solved the problem - the question now is, why toSource() didn't give me the info; PHP's var_dump would've done so ! :) the DOM;

<div><input type="text" size="3" name="elem_one" value="54" /></div><div><input type="text" size="3" name="elem_two" value="23" /></div><div><button onClick="tryXMLhtpReq()">tester</button></div>

the JS;

function tryXMLhtpReq()() {	var one=document.getElementsByName('elem_one');	var two=document.getElementsByName('elem_two');// alert(one.innerHTML); // = 'undefined' 	?// alert(one.nodeName);  // = 'undefined'	??// alert(one.toSource()); // = ({})	array of WHAT ??!// alert(one);	// = [object HTMLCollection]// alert(one[0]);	// = [object HTMLInputElement] // alert(one[0].tagName); // = 'INPUT'// alert(one[0].innerHTML);  // = ''// alert(one[0].toSource());  // = STILL ({}) array of WHAT ??!!alert(one[0].value);	  // = 54	DUH !!!}

in hindsight, using the value property FIRST or earlier on would've saved me the frustration...just a bit miffed that i didn't get int(54) as var_dump would've done.

Edited by edGetItTypee
Link to comment
Share on other sites

The variable one is actually an object. {} is object, [] is array.But the reason you didn't get an integer like you were expecting is because one is actually a reference to a DOM element collection object. one[0] is a reference to the first element in that collection. So what you saw for the output of toSource is actually correct. If you were to try testing the value of one[0], you should get an integer, ie, alert(one[0].value.toSource);

Edited by ShadowMage
Link to comment
Share on other sites

A var_dump as you're describing would NOT have reported the data type as an integer. The value property of all form controls contains text, plain and simple. It is up to your app to figure out if the data needs to be cast into another type of data.JavaScript will of course automatically typecast when it can, but auto-casting a string to a number sometimes leads to trouble.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...