0

want to make values of the oject's dynamic (from user input) but I get "undefined". The idea is to have 3 input fields and the user should input values in them which will fill up the alert message.

<script type="text/javascript">
function Family (fatherName, motherName, sisterName) {

    this.fatherName = fatherName;
    this.motherName = motherName;
    this.sisterName = sisterName;
    this.myFamily = function() {
        alert("My father's name is " + this.fatherName +", my mother's name is "+ this.motherName +" and my sister's name is " + this.sisterName +".");
    }

}

var Me = new Family(
    Family["fatherName"] = father,
    Family["motherName"] = mother,
    Family["sisterName"] = siter);

var father = document.getElementById("fatherId").value;
var mother = document.getElementById("motherId").value;
var sister = document.getElementById("sisterId").value;
</script>

<input type="text" id="fatherId" />
<input type="text" id="motherId" />
<input type="text" id="fatherId" />
<input type="submit" value="Send" onclick="Me.myFamily();"> 

Also I'm looking for a way how user can add or remove properties (values in them, too).

2
  • 1
    Swap the order of var Me = .... and var father = ..... Commented Dec 2, 2011 at 22:57
  • I don't understand the last sentence. What properties are we talking about? Can you provide an use case? Commented Dec 2, 2011 at 23:39

2 Answers 2

2

There are a few things wrong with your code.

You've used your variables here

Family["fatherName"] = father,
Family["motherName"] = mother,
Family["sisterName"] = siter); // This should be sister by the way

before declaring them here

var father = document.getElementById("fatherId").value;
var mother = document.getElementById("motherId").value;
var sister = document.getElementById("sisterId").value; // Doesn't exist

Try switching the statements so you're declaring the variables first.

Also, there is no sisterId, you've used fatherId twice.

You're also calling javascript before the DOM is ready. If you're using jQuery, wrap your JS in

$(document).ready(function() { }

or if you want to stick with plain JS, try

window.onload = function() { }

You'll have to be more specific on what myFamily is supposed to do, since you haven't even mentioned that method.

Sign up to request clarification or add additional context in comments.

3 Comments

At first I declare the variables first, but then it doesn't work at all. Why caliing the function onload? Onload there aren't values yet. I fixed the IDs, 10x.
Strictly, it's not the declaration that's out of order but the assignment of values (which is in the same statement).
@RalitsaNikiforova—because your inputs don't have any values (or even exist) when the code runs. Give the inputs values and run the code after they are created in the DOM and it will work.
1

Here is the working snippet of your example.

<input type="text" id="fatherId" />
<input type="text" id="motherId" />
<input type="text" id="sisterId" />
<input type="submit" value="Send" id="submit" />

<script>
    function Family(fatherName, motherName, sisterName) {

      this.fatherName = fatherName;
      this.motherName = motherName;
      this.sisterName = sisterName;

      this.myFamily = function() {
          alert("My father's name is " + this.fatherName + 
                ", my mother's name is " + this.motherName + 
                " and my sister's name is " + this.sisterName + ".");
      };

    }

    document.getElementById("submit").onclick = function() {
        var father = document.getElementById("fatherId").value;
        var mother = document.getElementById("motherId").value;
        var sister = document.getElementById("sisterId").value;

        Me = new Family(father, mother, sister);
        Me.myFamily();
    }
</script>

All the mistakes are summarized very well by Brandon.

*EDIT: (anser to your comment)

Your code has two execution related problems.

  • <script> tags are executed immediately and therefore if you insert script before the <input> part then there are no input elements available for you to retrieve.
  • You want to retrieve values of the inputs, but those inputs contain data when user clicks on the submit and therefore must be read using .value() at the onclick time. If you try to read them outside the onclick part then they are accessed immediately during page load when the input fields are empty.

2 Comments

Thank you, it works! I want to ask only one thing more. In previous edited version there isn't document.getElementById("submit") and I have to reload to show values but now everything works fine. So does it means I have to define submit button first so there will be values when I click on submit?
@RalitsaNikiforova I had to edit my answer to fix the error, but it should all work now. I added more details into the answer about the correct placement of parts of your example. In case it is not clear feel free to ask :).

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.