Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Color Me Red or Green 2

Status
Not open for further replies.

bsquared18

Technical User
Jun 10, 2001
329
US
I'm using the code below as a template for multiple-choice test items. To tailor each test item, "comment 1" etc. will be manually replaced with a message that says either, your choice is right or your choice is wrong, or words to that effect.

What code do I have to add to a comment line to make these comment words arial, bold. and colored? Say, for example, arial, bold, and green? (Instead of the vanilla black text that it now has.)

Thanks!

Bill B.

________________________________________________________

<input type=hidden name=questions value=5 />
<input name=&quot;comment0&quot; type=hidden value=&quot;comment 1&quot; />
<input name=&quot;comment1&quot; type=hidden value=&quot;comment 2&quot; />
<input name=&quot;comment2&quot; type=hidden value=&quot;comment 3&quot; />
<input name=&quot;comment3&quot; type=hidden value=&quot;comment 4&quot; />
<input name=&quot;comment4&quot; type=hidden value=&quot;comment 5&quot; />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;0&quot; />choice 1<br />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;1&quot; />choice 2 <br />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;2&quot; />choice 3<br />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;3&quot; />choice 4<br />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;4&quot; />choice 5<br />
<br>

<input name=&quot;upDateBut&quot; type=button
value=&quot;Check Your Answer&quot; onClick=&quot;update(this.form)&quot; /><!--<input name=&quot;redo&quot;
type=&quot;Reset&quot; value=&quot;Try Again&quot; /> --></p><p>
<textarea name=&quot;Response&quot; rows=&quot;6&quot; cols=&quot;40&quot;
wrap=&quot;virtual&quot;></textarea><br /></p>
</form>
 
I don't understand : you want to change font for hidden fields ??? Water is not bad as long as it stays out human body ;-)
 
Since the textarea is named response I assume you want to set the fontcolor of this textarea. For the following script to work you have to give the textarea an ID (in this case Response):

document.getElementById('Response').style.color=&quot;green&quot;;
document.getElementById('Response').style.color=&quot;red&quot;;
 
Sorry, I didn't make my explanation clear: Below is the entire code, with a sample question, &quot;In what month does Christmas fall?&quot; included.

In this example, value=&quot;3&quot; (Dec.) is correct and the other choices are incorrect. What I'm aiming for, if it's possible, is that when someone (in this case) clicks on the value=&quot;3&quot; radio button and then clicks on &quot;check your answer&quot; that the message they get (&quot;That's right!&quot;) would be in GREEN. On the other hand, if they click one of the other radio buttons, the message (&quot;No. Try again.&quot;) would be in RED.

If there is a solution, please be specific as to where I should put what, since I have limited programming experience.

Thanks!

Bill B.

________________________________________________

<html>
<head>
<title>JavaScript Multichoice Question</title>
</head>
<body>

<!--NB: The following SCRIPT block is only needed once per page-->
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function getrad(radob, qns)
{
var value = null
for (var i=0; i<qns;i++){

if (radob.checked) {
value = i
break}
}
return value
}

function update(form)
{
var msg = &quot;&quot;
var cherce = getrad(form.answer, form.questions.value)
if(cherce == null)
{msg = &quot;You need to select one of the choices.&quot;}
else {
if(cherce == &quot;1&quot;)
{
msg = form.comment1.value
} else {
if(cherce == &quot;2&quot;)
{
msg = form.comment2.value
} else {
if(cherce == &quot;3&quot;)
{
msg = form.comment3.value
} else {
if(cherce == &quot;4&quot;)
{
msg = form.comment4.value
} else {
if(cherce == &quot;0&quot;)
{
msg = form.comment0.value
}
}
}
}
}
}
form.Response.value = msg

}
// -->
</SCRIPT>
<!--NB: The above SCRIPT block is only needed once per page-->

<!-- ITEM NUMBER HERE -->
<h2>Item N</h2>

<p>In what month does Christmas fall?</p>


<form>

<input type=hidden name=questions value=5 />
<input name=&quot;comment0&quot; type=hidden value=&quot;No. Try again.&quot; />
<input name=&quot;comment1&quot; type=hidden value=&quot;No. Try again.&quot; />
<input name=&quot;comment2&quot; type=hidden value=&quot;No. Try again.&quot; />
<input name=&quot;comment3&quot; type=hidden value=&quot;That's right!&quot; />
<input name=&quot;comment4&quot; type=hidden value=&quot;No. Try again.&quot; />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;0&quot; />Sept.<br />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;1&quot; />Oct.<br />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;2&quot; />Nov.<br />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;3&quot; />Dec.<br />
<input name=&quot;answer&quot; type=&quot;radio&quot; value=&quot;4&quot; />Jan.<br />
<br>

<input name=&quot;upDateBut&quot; type=button
value=&quot;Check Your Answer&quot; onClick=&quot;update(this.form)&quot; /><!--<input name=&quot;redo&quot;
type=&quot;Reset&quot; value=&quot;Try Again&quot; /> --></p><p>
<textarea name=&quot;Response&quot; rows=&quot;6&quot; cols=&quot;40&quot;
wrap=&quot;virtual&quot;></textarea><br /></p>
</form>
<br>
<!-- Link to the next question -->
<A HREF=&quot;xxxx.html&quot;><FONT FACE=&quot;arial&quot; SIZE=&quot;3&quot; COLOR=&quot;#7C0019&quot; onmouseover=&quot;this.style.color='#FF796D'&quot;
onmouseout=&quot;this.style.color='#7C0019'&quot;>
NEXT ITEM</FONT></A>




</body>
</html>


 
Add this at the end of your &quot;update&quot; function :
Code:
...
.
form.Response.value = msg
if (msg..charAt(0) == &quot;N&quot;)
  form.Response.style.color=&quot;red&quot;;
else
  form.Response.style.color=&quot;green&quot;;
Water is not bad as long as it stays out human body ;-)
 
Targol,

Sorry to be such a dummy, but when I tried placing your code in various locations, I kept getting error messages. Could you indicate exactly where in my code your code should be placed?

Much appreciated,

Bill B.
 
Code:
function update(form)
{
    var msg = &quot;&quot;
    var cherce = getrad(form.answer, form.questions.value)
    if(cherce == null)
    {msg = &quot;You need to select one of the choices.&quot;}
    else {
    if(cherce == &quot;1&quot;)
    {
    msg = form.comment1.value
    } else {
        if(cherce == &quot;2&quot;)
        {
        msg = form.comment2.value
        } else {
            if(cherce == &quot;3&quot;)
            {
            msg = form.comment3.value
            } else {
                if(cherce == &quot;4&quot;)
                {
                msg = form.comment4.value
                } else {
                    if(cherce == &quot;0&quot;)
                    {
                    msg = form.comment0.value
                    }
                }
            }
        }
    }
    }
form.Response.value = msg
if (msg..charAt(0) == &quot;N&quot;)
  form.Response.style.color=&quot;red&quot;;
else
  form.Response.style.color=&quot;green&quot;;

}

If that doesn't work give me the line and text of the error Water is not bad as long as it stays out human body ;-)
 
Targol,

I found the problem: The line

if (msg..charAt(0) == &quot;N&quot;)

Had two dots after &quot;msg.&quot; When I removed one dot, there were no error messages.

So, now all the comments are green. Definitely a step forward! But how do I get the fourth comment to be green and the other three to be red?

Bill B.
 
replace :
Code:
if (msg.charAt(0) == &quot;N&quot;)
by :
Code:
if (msg.indexOf(&quot;Try again&quot;) != -1)

Sorry, I made a mistake on the charAt method. it returns a Char and therefore can never be equals to a String like &quot;N&quot;. Water is not bad as long as it stays out human body ;-)
 
Targol,

Excellent! Thanks for sticking with me. You definitely know your stuff.

Bill B.
 
A little star for me ???? Water is not bad as long as it stays out human body ;-)
 
Now that we've got the colors, the problem is that they are very faded. Is there a way to specify Arial font, size=3, bold, for example? If not, c'est la vie.

Bill B.
 
Sorry, Targol. I usually do that but it slipped my mind.

BB
 
you can do it applying a style to your textarea. This example puts your font in arial bold :
Code:
textarea name=&quot;Response&quot; rows=&quot;6&quot; cols=&quot;40&quot;
wrap=&quot;virtual&quot; style=&quot;font-family : Arial; font-weight : bold;&quot;></textarea>

Note : instead of using the litteral values for the colors (&quot;red&quot; and &quot;green&quot;), you can use rgb values that you can tune this way (always on the same example) :
Code:
form.Response.style.color=&quot;#
RRGGBB
Code:
&quot;;
Where :
- RR is red channel value in hexa from 00 to FF.
- GG is green channel value in hexa from 00 to FF.
- BB is blue channel value in hexa from 00 to FF. Water is not bad as long as it stays out human body ;-)
 
If there are stars to be erned then here are the parts you should change:
replace:
<textarea name=&quot;Response&quot; rows=&quot;6&quot; cols=&quot;40&quot;
wrap=&quot;virtual&quot;></textarea>

with:
<label id='Response'> </label>


and:
form.Response.value = msg
if (msg..charAt(0) == &quot;N&quot;)
form.Response.style.color=&quot;red&quot;;
else
form.Response.style.color=&quot;green&quot;;

with:
if (msg.indexOf(&quot;Try again&quot;) != -1)
document.getElementById('Response').innerHTML=&quot;<font style='color:green'>&quot;+msg+&quot;</font>&quot;;
else
document.getElementById('Response').innerHTML=&quot;<font style='color:red'>&quot;+msg+&quot;</font>&quot;;

 
Hey, harmmeijer, don't take it bad. I know it's bad to claim but that's a thing I do for others too. (I posted many times in threads replying &quot;give a star to the man&quot; to posts like &quot;I love you, you're my god, you saved my life&quot;. This site has provided a &quot;client satisfaction rating&quot; of all persons answering to threads, so why not to use it ? Water is not bad as long as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top