Reputation: 55
I am trying to get an object value using the input element, but it is not working, this is what I tried
var operator = new Object();
operator.name = document.getElementById("name").value;
operator.country = document.getElementById("country").value;
operator.occupation = document.getElementById("occupation").value;
operator.status = document.getElementById("status").value;
alert(operator.name + operator.country + operator.occupation + operator.status);
It keeps alerting undefined.
<form action="process.php" method="post" onsubmit="return myValidate()" name="myform">
Name:
Country:
Occupation:
Status:
Upvotes: 1
Views: 60
Reputation: 10563
Try using toSource()
method which represents the source code of an object. It will return the object properties along with their values
alert(operator.toSource());
Here is an example Fiddle
Edit:
toSource()
does not work in Internet Explorer or Safari. It is not a good practice. So you can use
alert(operator.name.toString());
Upvotes: 1
Reputation: 539
I guess you alert before form is filled with values (we probably need to see form html code)
Is this code called on document load?
Upvotes: 0
Reputation:
I've just had a go here http://jsfiddle.net/47LRJ/ and it works fine. can you show your HTML
<form>
<input id="text" type="text" value="text" />
</form>
var obj = new Object();
obj.text = document.getElementById("text").value;
alert(obj.text);
Upvotes: 1