2

I created some forms and dropdowns and got Javascript to find the values of the forms and dropdowns.

However, when I try to print these values I get either the default value of the dropdown or 'undefined' for a form, even when I've put text into them. How can I update the values?

There are several similar questions posted, I know, but I don't know whether their answers are relevant to my situation because I am new to Javascript.

Anyway the full page can be found via pastebin here: http://pastebin.com/DpcrsfxN.

1
  • value gets the selected value. also, put some simplified code in the question Commented Jan 3, 2012 at 5:06

1 Answer 1

2

Instead of

var oDayField = oForm.elements["day"]

Use

var oDayField = oForm.day;

and then to update the value of this input you'd simply do

oDayField.value = "New Value";

Also, it looks like you have this script right out in the open, after your body section:

var oReplacementNameField = oForm.elements["repname"]
var repname = oReplacementNameField.value;

function printVariables() {
        document.write(repname)
}

This will always print the default value of the input when the user submits the form because you're reading and saving this value right after the page is rendered.

You'll want to read these values fresh from inside of the printVariables function

var oForm = document.forms["postgen"];
var oReplacementNameField = oForm.repname;

function printVariables() {
    var repname = oReplacementNameField.value;

    document.write(repname)
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is very handy +1, it looks that I will need to fav the question, just to find your reply. I dream on a future where I can fav answers (and not just questions)
@ajax333221 - thanks for the kind words - glad the answer helped!
Okay so I made the changes you suggested. When I hit the Generate button, nothing happens. How do I link the form, the button, and the Javascript up so that upon hitting the button, I actually get output in the form of repname? Thanks for all your work.
Thanks for showing me how to read variables from inside the function, at any rate!

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.