I just can’t seem to figure out a search criteria that will point me to a thread that covers this. I have about 30 select boxes with the same exact options list. It’s a worksheet where kids are supposed to match elements from 2 columns of data. The column on the right stays fixed. The column on the left are Select Boxes with a list of all the possible answers. The kids pick from the list and correct answers are identified with a js function. The answer checking part I’ve got working fine.
I’d rather not repeat the code for 30 identical select boxes with 30 identical options. I guess if I have to, I will. The only difference in the Select objects are their Name=xxx.
Oh, and I did just find a few things elsewhere on the web, but I think it’s important to note that these select boxes are tabledata in one table with 2 columns. So a second tabledata element with text descriptions needs to follow each select box. I am thinking that might narrow down the useful approaches.
Any ideas?
[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
function CloneSelects(cloneid,tableid,inrow,incell){
var clonesel=document.getElementById(cloneid).getElementsByTagName('SELECT')[0];
var table=document.getElementById(tableid);
var rows=table.rows;
for (var zxc0=0;zxc0<rows.length;zxc0++){
if (inrow.match(zxc0)){
var cells=rows[zxc0].cells
for (var zxc1=0;zxc1<cells.length;zxc1++){
if (incell.match(zxc1)){
var newsel=clonesel.cloneNode(true);
newsel.name='Sel'+zxc0+zxc1;
newsel.options[0].text=newsel.name; // for demonstration only
cells[zxc1].appendChild(newsel);
}
}
}
}
}
//-->
</script>
</head>
<body onload="CloneSelects('CloneID','Tst1','1,3','0,2')">
<table id="Tst1" cellpadding="0" cellspacing="0" border="1">
<tr>
<td>Whatever</td>
<td>Whatever</td>
<td>Whatever</td>
<td>Whatever</td>
</tr>
<tr>
<td> </td>
<td>Whatever</td>
<td> </td>
<td>Whatever</td>
</tr>
<tr>
<td>Whatever</td>
<td>Whatever</td>
<td>Whatever</td>
<td>Whatever</td>
</tr>
<tr>
<td></td>
<td>Whatever</td>
<td></td>
<td>Whatever</td>
</tr>
</table>
<div id="CloneID" style="position:absolute;visibility:hidden;" >
<select style="width:100px;">
<option value="" >1</option>
<option value="" >2</option>
<option value="" >3</option>
<option value="" >4</option>
<option value="" >5</option>
<option value="" >6</option>
<option value="" >7</option>
</select>
</div>
</body>
</html>[/CODE]
<i>
</i><script>
function checkAnswer(fieldValue, correctAnswer) {
if (fieldValue == correctAnswer) { alert("correct"); }
else { alert("wrong"); }
}
</script>
What color is the sky?
<select onchange="checkAnswer(this.value,'blue');">
<option value="red">red</option>
<option value="blue">blue</option>
</select>
How many pennies are in a dollar?
<select onchange="checkAnswer(this.value,'100');">
<option value="100">100</option>
<option value="200">200</option>
</select>
CloneSelects('CloneID','Tst1','1,3','0,2')"[/QUOTE]
function checkOneMatch(selOb)
{
answer = selOb.name.substr(7);
selOb.className = (selOb.value==answer)?"dropTagsRight":"dropTagsWrong";
if (selOb.className=="dropTagsWrong") { selOb.selectedIndex = 0; }
}
function checkAllMatches(theForm)
{
for (i=0; i<theForm.elements.length; i++) {
if (theForm.elements[i].name.substr(0,6)=="answer") {
checkOneMatch(theForm.elements[i]);
}
}
return false;
}
function fillSelects() {
df = document.entry;
for (i=0; i<df.elements.length; i++) {
fldName = df.elements[i].name;
if (fldName.substr(0,6)=="answer" && fldName!="answer_html") {
for (j=0; j<df.answer_html.options.length; j++) {
txt = df.answer_html.options[j].text;
val = df.answer_html.options[j].value;
df.elements[i].options[j] = new Option(txt,val);
}
}
}
}
<script language="JavaScript" type="text/javascript">
<!--
function checkOneMatch(selOb)
{
guess = selOb.name.substr(5);
if (selOb.value==guess)
{
selOb.className = "dropTagsRight";
}
else
{
selOb.className = "dropTagsWrong";
}
}
function clearMatchedOptions(selOb, removeArray)
{
for (var i=selOb.options.length-1; i>=0; i--)
{
for (var j=0; j<removeArray.length; j++)
{
if (selOb.options[i].value==removeArray[j])
{
selOb.remove(i);
selOb.selectedIndex = 0;
}
}
}
}
function cheatFunction(theForm)
{
var guessObs = new Array();
for (i=0; i<theForm.elements.length; i++)
{
if (theForm.elements[i].name.substr(0,5)=="guess")
{
guessObs.push(theForm.elements[i]);
}
}
for (i=0; i<guessObs.length; i++)
{
for (var j=0; j<guessObs[i].options.length; j++)
{
if (guessObs[i].name.substr(5) == guessObs[i].options[j].value)
{
guessObs[i].selectedIndex = j;
}
}
}
checkAllMatches(theForm);
return (false);
}
function checkAllMatches(theForm)
{
var guessObs = new Array();
var goodGuesses = new Array();
var badGuesses = new Array();
for (i=0; i<theForm.elements.length; i++)
{
if (theForm.elements[i].name.substr(0,5)=="guess")
{
guessObs.push(theForm.elements[i]);
}
}
for (i=0; i<guessObs.length; i++)
{
checkOneMatch(guessObs[i]);
if (guessObs[i].className=="dropTagsRight")
{
goodGuesses.push(guessObs[i].value);
}
else
{
badGuesses.push(guessObs[i].value);
}
}
for (i=0; i<guessObs.length; i++)
{
if (guessObs[i].className=="dropTagsWrong")
{
clearMatchedOptions(guessObs[i], goodGuesses);
}
}
if (badGuesses.length==0)
{
alert ("Nice work, you got them all! Future Clue: Click on the word 'CHEAT' to see all the correct answers");
}
else
{
alert ("You only have " + badGuesses.length + " Tag remaining to correct.");
}
return (false);
}
//--></script>
[CODE]function setAnswer(divID)
{
var answer = cboOptions.options[cboOptions.selectedIndex].value;
document.getElementById(divID).InnerHTML = answer;
}[/CODE]
<html>
<head>
<script type="text/javascript">
var end = new Array('Html','Comment','Head','Title','Meta','Link','Body','IFrame','Div','Script','Form','Select','Option','Input','Table','Tr','Td','A','Img','Ol','Ul','Li','P','Br','B','I','H1','H2');
function makeFormData(){
var inc = 0;
var sel, op;
var opts = new Array();
var trs = document.getElementById('maintable').getElementsByTagName('tr');
opts[0]=new Array();
opts[1]=new Array();
opts[2]=new Array();
opts[3]=new Array();
opts[4]=new Array();
opts[5]=new Array();
opts[6]=new Array();
opts[7]=new Array();
opts[8]=new Array();
opts[9]=new Array();
opts[10]=new Array();
opts[11]=new Array();
opts[12]=new Array();
opts[13]=new Array();
opts[14]=new Array();
opts[15]=new Array();
opts[16]=new Array();
opts[17]=new Array();
opts[18]=new Array();
opts[19]=new Array();
opts[20]=new Array();
opts[21]=new Array();
opts[22]=new Array();
opts[23]=new Array();
opts[24]=new Array();
opts[25]=new Array();
opts[26]=new Array();
opts[27]=new Array();
opts[28]=new Array();
opts[29]=new Array();
opts[0][0] = "Blank";
opts[0][1] = " ";
opts[1][0] = "Link";
opts[1][1] = "&lt;link&gt;...&lt;/link&gt;";
opts[2][0] = "I";
opts[2][1] = "&lt;i&gt;...&lt;/i&gt;";
opts[3][0] = "Li";
opts[3][1] = "&lt;li&gt;...&lt;/li&gt;";
opts[4][0] = "Body";
opts[4][1] = "&lt;body&gt;...&lt;/body&gt;";
opts[5][0] = "Img";
opts[5][1] = "&lt;img&gt;...&lt;/img&gt;";
opts[6][0] = "Table";
opts[6][1] = "&lt;table&gt;...&lt;/table&gt;";
opts[7][0] = "Select";
opts[7][1] = "&lt;select&gt;...&lt;/select&gt;";
opts[8][0] = "Br";
opts[8][1] = "&lt;br&gt;...&lt;/br&gt;";
opts[9][0] = "H2";
opts[9][1] = "&lt;h2&gt;...&lt;/h2&gt;";
opts[10][0] = "Html";
opts[10][1] = "&lt;html&gt;...&lt;/html&gt;";
opts[11][0] = "IFrame";
opts[11][1] = "&lt;iframe&gt;...&lt;/iframe&gt;";
opts[12][0] = "Tr";
opts[12][1] = "&lt;tr&gt;...&lt;/tr&gt;";
opts[13][0] = "P";
opts[13][1] = "&lt;p&gt;...&lt;/p&gt;";
opts[14][0] = "Title";
opts[14][1] = "&lt;title&gt;...&lt;/title&gt;";
opts[15][0] = "Input";
opts[15][1] = "&lt;input&gt;...&lt;/input&gt;";
opts[16][0] = "B";
opts[16][1] = "&lt;b&gt;...&lt;/b&gt;";
opts[17][0] = "Comment";
opts[17][1] = "&lt;&#33;--...--&gt;";
opts[18][0] = "H1";
opts[18][1] = "&lt;h1&gt;...&lt;/h1&gt;";
opts[19][0] = "Ol";
opts[19][1] = "&lt;ol&gt;...&lt;/ol&gt;";
opts[20][0] = "Meta";
opts[20][1] = "&lt;meta&gt;...&lt;/meta&gt;";
opts[21][0] = "Td";
opts[21][1] = "&lt;td&gt;...&lt;/td&gt;";
opts[22][0] = "Option";
opts[22][1] = "&lt;option&gt;...&lt;/option";
opts[23][0] = "Ul";
opts[23][1] = "&lt;ul&gt;...&lt;/ul&gt;";
opts[24][0] = "Head";
opts[24][1] = "&lt;head&gt;...&lt;/head&gt;";
opts[25][0] = "Div";
opts[25][1] = "&lt;div&gt;...&lt;/div&gt;";
opts[26][0] = "A";
opts[26][1] = "&lt;a&gt;...&lt;/a&gt;";
opts[27][0] = "Form";
opts[27][1] = "&lt;form&gt;...&lt;/form&gt;";
opts[28][0] = "Script";
opts[28][1] = "&lt;script&gt;...&lt;/script&gt;";
for(var i=0; i<29; i++)
{
sel = document.createElement('select');
sel.id="sel"+i;
tds = trs[i].getElementsByTagName('td');
tds[0].appendChild(sel);
for(j=0;j<29;j++)
{
op = document.createElement('option');
op.value = opts[j][0];
op.text = opts[j][1];
s = document.getElementById('sel'+i);
s.options[s.options.length] = op;
}
}
return true;
}
function checkIt(){
var f = document.getElementById('myform');
var sels = f.getElementsByTagName('select');
var correct = 0;
for(var k=0;k<sels.length;k++)
{
if(sels[k].value == 'Blank')
{
alert('You did not answer question #'+(k+1));
return false;
}
else if(sels[k].value == end[k])
correct++;
}
alert('You got ' +correct+ ' correct out of 28.');
return true;
}
</script>
</head>
<body onload="makeFormData()">
<form id="myform">
<table class=awardsTable id="maintable" border="2">
<col width=165px><col width=70%>
<tr>
<td><i>Tag:</td><td><i>Functional Description:</td>
<tr>
<td>
</td>
<td> All of the HTML content within the HTML file. Text outside of this tag area is not interpreted as HTML.
</td>
</tr>
<tr>
<td>
</td>
<td> A Comment. These are notes in your HTML file that make your code easier for you and others to read. Comments are not interpreted as HTML, and therefore do not show up on the page, nor perform any function.
</td>
</tr>
<tr>
<td>
</td>
<td> The Header is the beginning of the HTML content. This section contains the TITLE, LINK, and META elements in our 4H web pages.
</td>
</tr>
<tr>
<td>
</td>
<td> The Title is the text that appears in your browser's title bar, in the very top left corner of the window.
</td>
</tr>
<tr>
<td>
</td>
<td> Meta data are words and descriptions intended to be found by search engines. For example our main page has the following meta keywords: "Pacifica 4H, Pacifica 4-H, 4H, 4-H, Kids, Animals, Raising". Our meta description is "Pacifica 4H website home page. Here you see and learn about our club. Hope you can join us...".
</td>
</tr>
<tr>
<td>
</td>
<td> Links to code outside the document. For example, our global CSS file is p4h.css, and our global javascript file is p4h.js. Linking to one location means we only have to make a change in one document to effect the global formatting or functioning of all our documents. <a class="p4h2">This sentence is formatted with our "p4h2" style, defined in p4h.css only, but used throughout our website.
</td>
</tr>
<tr>
<td>
</td>
<td> The Body of the document. What actually appears on screen.
</td>
</tr>
<tr>
<td>
</td>
<td> An In-Line Frame. This is a rectangular region of content that can be placed anywhere on the page. Objects and text can flow or be located at specific locations within this region, as opposed to being relative to the full page boundaries.
</td>
</tr>
<tr>
<td>
</td>
<td> Divisions within the web page. These divisions can be formatted in the page or through your .CSS (Cascading Style Sheet) file. It is a very powerful tool to format your pages.
</td>
</tr>
<tr>
<td>
</td>
<td> JavaScript code. This is a programming language designed to work within, and on, web pages.
</td>
</tr>
<tr>
<td>
</td>
<td> A Form in which users can enter data into the page, or select from lists of options.
</td>
</tr>
<tr>
<td>
</td>
<td> A box in which users select from a list of options, like the one you are about to click on.
</td>
</tr>
<tr>
<td>
</td>
<td> One of the options in a list of choices in a SELECT box, like the correct selection you are about to make.
</td>
</tr>
<tr>
<td>
</td>
<td> Push these input buttons, <input type="radio" name="demoRadio" value="Yes"> to make things happen or select between options <input type="radio" name="demoRadio" value="No">.
</td>
</tr>
<tr>
<td>
</td>
<td> A rectangular table with rows and columns.
</td>
</tr>
<tr>
<td>
</td>
<td> A table row.
</td>
</tr>
<tr>
<td>
</td>
<td> Table data. The content (images, text, etc.) that goes in a table cell.
</td>
</tr>
<tr>
<td>
</td>
<td> An anchor, otherwise known as a hypertext link. These can contain images or text that you click on to get sent to another location. Don't <a href="htmlHome.html">click on this one</a>, or you will have to start your excercise over.
</td>
</tr>
<tr>
<td>
</td>
<td> An image, such as <img src="../../p4hPushpins/p4h12.png">
</td>
</tr>
<tr>
<td>
</td>
<td> <ol><li>An</li><li>ordered (numbered)</li><li>list</li></ol>
</td>
</tr>
<tr>
<td>
</td>
<td> <ul><li>An</li><li>unordered (bulleted)</li><li>list</li></ol>
</td>
</tr>
<tr>
<td>
</td>
<td> A list item, such as "unordered" in the functional description above.
</td>
</tr>
<tr>
<td>
</td>
<td> A new paragraph.
</td>
</tr>
<tr>
<td>
</td>
<td> A line break.
</td>
</tr>
<tr>
<td>
</td>
<td> <B>Bolded text</b>
</td>
</tr>
<tr>
<td>
</td>
<td> <i>Italicized text</i>
</td>
</tr>
<tr>
<td>
</td>
<td> <H1>The largest standard Heading style.</H1>
</td>
</tr>
<tr>
<td>
</td>
<td> <H2>The 2nd largest standard Heading style.</H2>
</td>
</tr>
</table>
<input type="button" onclick="checkIt()" value="Submit">
</form>
</body>
</html>
[CODE]<tr><td><select class="DropTagsRight" size="1" name="guessI">
<!--#include FILE="htmlTagWorksheetOptions.asp"-->
</select></td>
<td> Italicized text.</td></tr>[/CODE]
0.1.9 — BETA 5.18