Jump to content
Larry Ullman's Book Forums

Referencing Form Elements 143 - 150


Recommended Posts

The Switch Conditional exercise on pages 143 - 150 makes us of an undefined variable, var cost; and I'm not sure what role it plays in the code. Do the cost elements in the switch statements load the value into the undefined variable var cost depending upon which condition is met in the document's type value?

 

I can remove these two lines of code, as well as the undefined variable, var cost, and still see the membership form run:

 

 

 var type = document.getElementById('type');
 var years = document.getElementById('years');

 

Why did we create a reference to the document element years when it seems not to be used in the script?

 

 

Many mysteries have unfolded in this exercise.

 

Link to comment
Share on other sites

The Switch Conditional exercise on pages 143 - 150 makes us of an undefined variable, var cost; and I'm not sure what role it plays in the code. Do the cost elements in the switch statements load the value into the undefined variable var cost depending upon which condition is met in the document's type value?

Yes, that's exactly it. The cost variable is set based on the condition met by the switch statement.

 

I can remove these two lines of code, as well as the undefined variable, var cost, and still see the membership form run:

What do you mean by this? In what respect does it run? I'm willing to bet that if you erase everything you mentioned and then submit the form, either an error will occur or you will not get the intended result.

 
The type and years variables are used to store the values entered for the corresponding inputs on the form. Without type, you can't execute the switch statement, and without years, you can't calculate the proper cost.
In other words, they're both required.
Link to comment
Share on other sites

 Share

×
×
  • Create New...