/    Sign up×
Community /Pin to ProfileBookmark

alert not showing!!

hi guys

this is probably real simple to you but im a newbie …if you look at my code at the discount part of the script, if no radio button is selected it doesnt seem to be bringing up my alert but instead carrys on with the calculation. your help is appreciated!!

thanks, marty.


*****************

<html>
<head>
<title>New Zealand Holiday Price Calculator</title>

<script language=”JavaScript”>
<!–

function tourType() //this is the fucntion that will calculate the selected fields in the form
{
var payment=0; //declare the variable payment to be equal to zero
if (document.info_form.optlist.selectedIndex == 0) //this checks that an option has been slected from the dropdown box
window.alert(“You have not selected a tour type”); //if not this alert will show
else
if (document.info_form.optlist.selectedIndex == 1)
{
payment=1500; //if option 1 is selected payment becomes 1500
}
else
if (document.info_form.optlist.selectedIndex == 2)
{
payment=1600; //if option 2 is selected payment becomes 1600
}
else
if (document.info_form.optlist.selectedIndex == 3)
{
payment=1750; //if option 3 is selected payment becomes 1750
}
else
if (document.info_form.optlist.selectedIndex == 4)
{
payment=2100; //if option 4 is selected payment becomes 2100
}

discount=0; //the variable discount is set to zero
for (var i=0; i<=3; i++) // this for loop is checks to see which radio button was selected by the user

if (document.info_form.dateDiscount[i].checked)
{
discount=document.info_form.dateDiscount[i].value; //dependant on the radio button slected, its value, stated in the html

code becomes the integer for the variable discount
}
else
if (document.info_form.dateDiscount == 0)
{
alert(“No date selected”);
}

var airtravel=0; //this is the variable for the checkbox
if (document.info_form.airfare.checked) //if the checkbox named “airfare” has been checked
{
airtravel=400; //then the variable becomes 400
}

var total=payment+airtravel-discount; //a new variable is stated here. it adds all the values from the previous variables
alert(“The approximate cost is $”+total); //an alert box is then shown to show the user the cost of the holiday
}

//–>
</script>

</head>
<body>
<font size=”6″><b>New Zealand Price Holiday Calculator</b>
<p>
<font size=”3″>
Complete the form below to calculate the cost of your New Zealand Holiday.
<p>
<form name=”info_form”>
Tour Type:
<select name=”optlist”>
<option>
<option>12-day New Zealand Escape Tour
<option>14-day New Zealand Panorama Tour
<option>15-day New Zealand South to North Tour
<option>17-day Best of New Zealand Tour
</select>
<p>
Payment date:<br>
<input type=”radio” name=”dateDiscount” value=”200″>Before 1st August 2005
<br>
<input type=”radio” name=”dateDiscount” value=”150″>Between 1st and 31st August 2005
<br>
<input type=”radio” name=”dateDiscount” value=”100″>Between 1st and 30th September 2005
<br>
<input type=”radio” name=”dateDiscount” value=”0″>After 30th September 2005
<p>
Click here if you want to include an airfare from Sydney, Brisbane or Melbourne: <input type=”checkbox” name=”airfare”>
<p>
<input type=”submit” value=”Calculate” onclick=”tourType();”>&nbsp;<input type=”reset” value=”Reset”>
</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJun 10.2005 — if (document.info_form.optlist.selectedIndex == 4)

{

payment=2100; //if option 4 is selected payment becomes 2100

}

[color=red]

var discount=-1;

var rad = document.info_form.dateDiscount;// the radio collection

for (var i=0; i<rad.length; i++){

if(rad[i].checked){

discount=Number(rad[i].value); break

}

}

if (discount==-1){

alert("No date selected");

else{

var airtravel=0;

if (document.info_form.airfare.checked){airtravel=400;}

var total=payment+airtravel-discount;

alert("The approximate cost is $"+total);

}

}

[/color]




Take care: the elements' values are [b]strings[/b], not numbers, so before any addition/substracting transform them in numbers. One of the method is [b]Number()[/b].
Copy linkTweet thisAlerts:
@cusimar9Jun 10.2005 — Another point to consider is that if there's a javascript error the browser usually just bombs out of whatever function you're in. So that could be why your alert isn't showing at all.

Make use of Firefox's Javascript Console, its helped me out countless times ?
×

Success!

Help @tyronemarty spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...