How to use Confirmation boxes to help select options?
Hi,
I am trying to do some tutorials in JavaScript and the following code has been given in the help document:
Code:
<SCRIPT>
function confirm(){
var y = "Your address ";
y=y+order.address.value;
y=y+" Your toppings";
if (document.order.chkolives.checked){
y = y + " olives";
}
if (document.order.chkchick.checked){
y = y + " spicy chicken";
}
if (document.order.chkspam.checked){
y = y + " spam";
}
if (document.order.chkyuck.checked){
y = y + " beetroot and mango YUCK!";
}
if (document.order.chktyres.checked){
y=y+" Old Tyres And Razor blades, OUCH!";
}
alert(y);
}
function finalise(){
if (document.order.chkyuck.checked == 0){
if (confirm ("why not try our half price beetroot and mango promotion?")){
document.order.chkyuck.checked == 1;
}
}
}
</SCRIPT>
<BODY>
<FORM NAME="order">
<H1>Enter your address</H1>
<INPUT TYPE TEXT SIZE=80 NAME="address">
<P><P>
<H1>Choose your toppings</H1>
<INPUT TYPE="checkbox" NAME="chkolives">Olives
<P>
<INPUT TYPE="checkbox" NAME="chkchick">Spicy Chicken
<P>
<INPUT TYPE="checkbox" NAME="chkspam">Spam
<P>
<INPUT TYPE="checkbox" NAME="chkyuck">Beetroot and Mango
<P>
<input type="checkbox" name="chktyres">Old Tyres And Razor Blades<p><p>
<INPUT TYPE="button" VALUE="confirm order"
ONCLICK = confirm()>
<INPUT TYPE="button" VALUE="finalise"
ONCLICK = finalise()>
</BODY>
</HTML>
And the idea is that if the user does not select the Beetroot And Mango option when they click the Finalise button, it alerts them and if they click on OK, then the option is selected automatically. However, the coding given does not seem to work and I was wondering if there was a way of fixing it.
First of all, you overwrote the default confirm function in JS. That's why you have to rename your function to something else. Let's say confirmFn. So you have
Code:
function confirmFn(){
alert('confirm');
var y = "Your address ";
...
Second, make difference between
== (equal)
and
= (assign)
In your finalize function, you must have one equal and one assign. You have two equals. Use this
Code:
function finalise(){
if (document.order.chkyuck.checked == 0){
if (confirm ("why not try our half price beetroot and mango promotion?")){
document.order.chkyuck.checked = 1;
}
}
}
If this is part from a tutorial and you will have a lot to write, I'd recommand you to learn jQuery. It's really 'write less, do more'
Bookmarks