Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,337 members, 7,780,885 topics. Date: Friday, 29 March 2024 at 02:35 AM

Can Anybody Debug This Javascript? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Can Anybody Debug This Javascript? (1140 Views)

Someone Explain This Javascript Snippet / Please Help Debug This Javascript Program / When Your Brain Throws Stackoverflow Error - Debug This JAVA Code If You Can (2) (3) (4)

(1) (Reply) (Go Down)

Can Anybody Debug This Javascript? by nnamokenna(m): 10:37pm On Sep 20, 2016
Hi all.

I have this JavaScript code that has been giving me headache.

The code is this:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Untitled</title>
</head>

<body>


<script language="javascript">
function AddInputs()
{
var total = 0;
var total1 = 0;
var add_menu_item1 = 1.50;
var add_menu_option2 = 0.50;
var managed_sms4 = 27.00;
var table_tent5 = 4.75;

var coll = document.getElementsByTagName(" input " )
for ( var i = 0; i<coll.length; i++)
{ if (i == 1) {

var temp = coll[i];

var temp1 = temp * add_menu_item1;

var ele1 = temp1;
}
else { var ele1 = coll[i]
}
var ele = coll[i];
total += parseFloat(ele.value);
total1 += parseFloat(ele1.value);

}
var Display = document.getElementById("Display " );
Display.innerHTML = total;

var Display1 = document.getElementById(" Display1 " );
Display1.innerHTML = total1;


}

function submitform()
{
document.forms["myform"].submit();
alert("Value is sumitted " );
}
</script>


<form id="myform" method="post">
<br><b>Email where you would like to receive your quote.</b>
<br><textarea name="email" cols="40" rows="1"></textarea>
<br><input name="site_setup" type="hidden" value="250.00" onkeyup="AddInputs()"> Site setup $250.00 Includes 50 menu items with upto 4 addons and 5 options.
<br><input name="additional_count" value="0" onkeyup="AddInputs()"> Additional Menu Items
<br> <input name="option_count" value="0" onkeyup="AddInputs()"> Additional Options
<br><input name="hosting" value="45.00" onkeyup="AddInputs()"> Prepaid Monthly Hosting
<br><input name="managed_sms_count" value="0" onkeyup="AddInputs()"> Number of Managed SMS Broadcasts
<br><input name="table_tent_count" value="10" onkeyup="AddInputs()"> Number of Table Displays
<br><input name="photography" value="400.00" onkeyup="AddInputs()"> Photography per Day.
<br><input name="vip" value="150.00" onkeyup="AddInputs()"> VIP Promotion
<br><input name="viral_referral" value="150.00" onkeyup="AddInputs()"> Viral Referral
<br><input name="loyalty" value="150.00" onkeyup="AddInputs()"> Loyalty Point Promotion
<br><input name="broadcast" value="150.00" onkeyup="AddInputs()"> SMS Broadcast
<br><input name="printer" value="150.00" onkeyup="AddInputs()"> 6 Months Monthly printer at $25.00/Month.

<br><textarea cols="10" rows="1" name="display" id="Display"></textarea> <b>Total</b>
<br><textarea cols="10" rows="1" name="display1" id="Display1"></textarea> <b>Total1</b>

<br><br><a href="javascript: submitform()"><b>Submit</b></a>

</form>
</body></html>


The Total field should give the total of the values in the input boxes while Total1 should give the total of the values in the input boxes, but when you change the value of "Additional Menu Items" field, the field should be multiplied by 1.5 (that is 1.5 x the value). It is giving NaN presently.

I have tried everything but can seem to get it to work. Any help would be appreciated.

Thanks

PS: I attached the file

Re: Can Anybody Debug This Javascript? by Nobody: 11:27pm On Sep 20, 2016
The reason is because some of the values you are trying to add up is returning NAN.

So change this line
total1 += parseFloat(ele1.value);
to
if(ele1.value !==undefined) {total1 += parseFloat(ele1.value);}
and that should do the trick

1 Like

Re: Can Anybody Debug This Javascript? by nnamokenna(m): 11:47pm On Sep 20, 2016
dhtml18:
The reason is because some of the values you are trying to add up is returning NAN.
So change this line
to
and that should do the trick

Thank you for your time but I have tried that before without success. Whatever value you put in the "Additional Menu Items" should be multiplied by 1.5 before being summed with the other values in the other fields to give Total1.

So in essence, the value of Total1 should be more than Total but with this way it (Total1) is being reduced by the value you put in "Additional Menu Items".
Re: Can Anybody Debug This Javascript? by Nobody: 12:00am On Sep 21, 2016
I have attached the entire correction something to this post. Works fine at my end, even in the EVIL INTERNET EXPLORER.
If there is any other thing, it might be a logical problem.

Re: Can Anybody Debug This Javascript? by Kodejuice: 3:43am On Sep 21, 2016
replace ' var temp = coll[i] ' with 'var temp = coll[i].value', thats the source of the NaN, i haven't ran it yet, i just used my built in debugger. smiley


the error occured when you multiplied an html element with a numerical value, so ur browser dey complain.

Error =>
 var temp1 = temp * add_menu_item1 

1 Like

Re: Can Anybody Debug This Javascript? by nnamokenna(m): 7:16am On Sep 21, 2016
Thank you very much @dhtml18 and @Kodejuice for your assistance. It is working now.
Re: Can Anybody Debug This Javascript? by Nobody: 8:08am On Sep 21, 2016
You are welcome.

(1) (Reply)

Do I Need To Install Node.js In Order To Code With It ? / Programmer Needed Urgently BOWEN UNIVERSITY, IWO Osun State / Need Competent Programmers In PH

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.