I am working through an assignment and I have run into a problem that is weird to me and that I have no idea how to solve. It is hard to explain but hopefully I'll be able to explain it well enough for someone to understand. I am working with HTML forms (radio buttons, check boxes, select boxes, text boxes) and my teacher gave us tasks to do with the HTML in javascript. I have been getting through the tasks but after a certain point, when I try to declare an object and pull some HTML from the forms, it says it is "null". However, if I put it at the top of the page it will work but then it cancels out anything under it. (i know this sounds confusing but maybe seeing some code might help..)
<html>
<body>
<script language="javascript">
<!--
function fred ()
{
option1=document.f1.zooanimal.option1
if(document.f1.game1.checked||document.f1.game2.checked||document.f1.game3.checked||document.f1.game4.checked)
{
return true
}
else
{
alert("Must Select at least ONE Checkbox Value!!!")
}
if (document.f1.zooanimal.selectedIndex=option1);
{
alert("Must Select Option other than default value!");
}
bigtextstr=document.f1.bigtext.value
bigedit=bigtextstr.replace(/ /g,"+")
bigedit2=bigedit.replace(/[\r\n]/g , "")
document.write("Characters in Text Area Before Edits="+"<br>")
biglen=bigtextstr.length
document.write(biglen+"<br>")
document.write("Characters in Text Area After Edits="+"<br>")
newbiglen=bigedit2.length
document.write(newbiglen+"<br>")
}
-->
</script>
<p>
<form name="f1">
<br>
Name <input type="text" name="nametext" size="30" value="First Last"><p>
List your favorite things to do <P><textarea name="bigtext" rows="5" cols="40">default value</textarea>
<p>What is your favorite animal to see at the zoo?
<select name="zooanimal">
<option name= "option1" selected="yes">default value
<option name="option2">elephants
<option name="option3">giraffes
<option name="option4">tigers
<option name="option5">seals
</select>
<p>
What is your favorite color?<br><p>
blue <input name="rb" type="radio" value="blue" checked> green <input name="rb" type="radio" value="green">
pink <input name="rb" type="radio" value="pink"> yellow <input name="rb" type="radio" value="yellow"> red <input name="rb" type="radio" value="red"> black <input name="rb" type="radio" value="black"></p>
Which of these games do you play?<br><p>
Starcraft <input name="game1" value="Starcraft" type="checkbox"> World of Warcraft <input name="game2" value="WorldofWarcraft" type="checkbox">
League of Legends <input name="game3" value="LeagueofLegends" type="checkbox"> none <input name="game4" value="none"
type="checkbox"><P>
<p><input type="button" value="EDIT AND REPORT" onClick="fred()">
<p>
<p>
</form>
</body>
</html>
Now looking at the script part, everything works up until that point but if i try to add any other form referencing after that, it says its null. I am very confused and have been working on this for several, several hours. Can someone please help?
document.write,document.nameOfElementinstead ofdocument.getElementById, missing semicolons, using thelanguageattribute, HTML comments to hide code, no end tags forpelements... Really, nothing personal, but this shows really bad practices.