I have been studying Javascript for quite a while now and I've recently started some projects of my own to try it out. Although my script looks perfectly valid to me, the error console in my browser keeps reporting that my object data type-variables are undefined or
null, but I explicitly declared them at beginning of my script. I have attached two of my projects in hopes that someone might notice the mistake I did and point it out to me and explain why it caused an error and show me the appropriate method. I've included comments in the script to explain what they are meant to do.
Here's the HTML:
<label for="nameField">Enter your name:
<input type="text" size="28" id="nameField">
</label>
<input type="submit" value="Sumbit"/> <br />
<div></div>
</form>
The script:
<label for="nameField">Enter your name:
<input type="text" size="28" id="nameField">
</label>
<input type="submit" value="Sumbit"/> <br />
<div></div>
</form>
//Obtain the form in the document
var theForm= document.getElementsByTagName('form');
//Get the form's only text box with the parent.firstChild method
var name=theForm.firstChild;
//Obtain the empty div element where we will greet the user with their name
var writingPlace=document.getElementsByTagName('div');
function writeGreeting(){
//If the text box holds a value
if (name.hasValue){
//Assign the variable 'greeting' with the word 'hi' and and their name,then
var greeting= 'Hi,' + name.value;
//Assign the contents of 'greeting' to the document's div element
writingPlace.appendChild(greeting);
}
}
//When the form is submitted, the function writeGreeting is called and the containg code is
//performed
theForm.onsubmit=writeGreeting;
Seems pretty overdone,huh?
The second script is a style switcher application:
The HTML:
<div id="mainContent">
<h2>Customizable Style-Sheets</h2>
<hr />
<h4>Choose a background pattern:</h4>
<table cellspacing="13" id="samples">
<tr>
<!--The theme samples-->
<!--leafy sample-->
<td><img src="leafy_sample.jpg" /></td>
<!--bluecubes sample-->
<td><img src="bluecubes_sample.jpg" /></td>
<!--leopardskin sample-->
<td><img src="leopardSkin_sample.jpg" /></td>
</tr>
<tr>
<!--lightning sample-->
<td><img src="lightning_sample.jpg" /></td>
<!--redcubes sample-->
<td><img src="redcubes_sample.jpg" /></td>
</tr>
</table>
</div>
The Javascript:
var body= document.getElementsByTagName('body');
//Declare the variable 'class4body' which will hold the class to be applied to the document's
//body
var class4body;
//The variable 'themes' holds all the image samples from the HTML
var themes= document.images;
//According to sample was clicked, class4body is assigned the theme name
themes[0].onclick=function applyLeafy(class4body){
class4body='leafy';
}
themes[1].onclick=function applyBluecubes(class4body){
class4body='bluecubes';
}
themes[2].onclick= function applyLeopardskin(class4body){
class4body='leopardskin';
}
themes[3].onclick=function applyLightning(class4body){
class4body='lightning';
}
themes[4].onclick=function applyRedcubes(class4body){
class4body='redcubes';
}
//The document's body is given the class of class4body's value
body.className=class4body;
The CSS:
body {background-color:black;}
#mainContent {width:6in;
position:absolute; left:330px; top:160px;
padding-top:16px;
padding-bottom:14px;
padding-left:18px;
background-color:#7595bc;
border:black 5px solid;
}
hr {width:520px;
text-align:left;}
h2{font-family:ae_AlMothnna;
font-weight:bold;
text-align:left;}
h4 {text-align:left;
font-family:ae_AlMothnna;
font-weight:300;
}
#samples td {border:purple 1px solid;}
[b]
body.leafy {background-image:url("leafy_sample.jpg");}
body.bluecubes {background-image:url("bluecubes_sample.jpg");}
body.leopardskin {background-image:url("leopardSkin.jpg");}
body.lightning {background-image:url("lightning_sample.jpg");}
body.redcubes {background-image:url("redcubes_sample.jpg");}
[/b]
Could someone please point out what is going wrong? I'd appreciate it.
--xaippo_script