1

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:

            

            

            

2
  • what errors on console? ..but it is not working? Commented Jan 16, 2014 at 8:32
  • not working means what? Commented Jan 16, 2014 at 8:36

3 Answers 3

1

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());
Sign up to request clarification or add additional context in comments.

6 Comments

What is .toSource()?.Are you talking about .toString()
@Pilot returns the object values
Is .toSource() is meant for function object only.I am getting uncaught TypeError: Object #<Object> has no method 'toSource'
.toSource() This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.*
|
1

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);

1 Comment

This looks similar to mine
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.