1

I am writing a simple javascript function that grabs information out of three text areas. Pretty basic, but for some reason javascript is telling me that my variables are not defined and I am confused as to why.

The html is as followed:

<input type = "text" id = "month" name = "month"/>
    <input type = "text" id = "day" name = "day"/>
    <input type = "text" id = "year" name = "year"/>
    <input type = "button" id = "submit" value = "Go!" onClick = "calculate(month, day, year)"/>

The javascript is simply

function calculate(month, day, year){
var setMonth = document.getElementById(month).value;
var setDay = document.getElementById(day).value;
var setYear = document.getElementById(year).value;}

And when I run this and click my button, it says "month is not defined" in Firebug. So I am a little confused, since ... they are defined!

6 Answers 6

9

month, day, and year are interpreted as variable names, which don't exist. You want to pass strings:

onClick = "calculate('month', 'day', 'year')"/>
Sign up to request clarification or add additional context in comments.

Comments

1

you did not define them: to do that you would do:

calculate('a', 'b', 'c');

in your case you need to change you code alot or change what your function does:

i would say do option 2:

onclick ="calculate(document.getElementById('month'), document.getElementById('day'), document.getElementById('year'));"

and the function to:

function calculate(month, day, year){
   var setMonth = month.value;
   var setDay = day.value;
   var setYear = year.value;
}

3 Comments

I like your answer better than mine, but I think I would pass the value instead of calculating it in the function - that way, the function can be used for forms, or just passing it the numbers.
@Dave i was just trying to keep sorta the same format the OP had already
completely understand. And like I said, I like your answer better than mine :) Just making notes in case the OP used yours - offering up a minor change.
1

Notice that the calculate function does not have any arguments and that the document is queried based on the name of the fields.

HTML

<input type = "text" id = "month" name = "month"/>
    <input type = "text" id = "day" name = "day"/>
    <input type = "text" id = "year" name = "year"/>
    <input type = "button" id = "submit" value = "Go!" onClick = "calculate()"/>

Javascript

function calculate(){
var setMonth = document.getElementById('month').value;
var setDay = document.getElementById('day').value;
var setYear = document.getElementById('year').value;}

Comments

0

In the "onClick" portion of your html, you're attempting to send "month", "day", and "year" variables that have yet to be defined.

Instead of passing the variables, try just running the "calculate();" function, and getting the data from the form from within the function.

Comments

0

When you call a function you must pass parameters initiates

<input type = "text" id = "month" name = "month"/> <input type = "text" id = "day" name = "day"/> <input type = "text" id = "year" name = "year"/> <input type = "button" id = "submit" value = "Go!" onClick = "calculate(document.getElementById(month), document.getElementById(day), document.getElementById(year))"/>

Javascript code:

function calculate(month, day, year){
var setMonth = month.value;
var setDay = day.value;
var setYear = year.value;}

Comments

0

if you want tu use the function $(document).ready(...)

you need to add in the <HEAD></HEAD> tag the folowing line:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

1 Comment

How does a question about javascript where there is no mention of jQuery result in this? And besides, you don't have to put your jQuery in the head, it is better to place it (and all other possible scripts) at the end of the page.

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.