Calculating / Populating Function - Percent Variable Showing Up NaN

Jul 1, 2011

I have 2 things going on here. One, the 'percent' variable is constantly showing up as NaN (not a number) no matter what numbers I give it. I suspect it is happening because the function is populating all the fields at one, which causes a 'divide by zero' scenario for the percentage field, which is last in line. Which brings me to my send thing. How to write this function correctly, as it obviously is causing problems as is, and it seems clunky. I think it should broken up into a couple different functions, but I tried that and NOTHING worked.

Here is the code, let me kow what you think:
function calculate(){
var contractsAdded = document.forms[0].contractsAdded.value;
var priceAdded = document.forms[0].priceAdded.value;
var contractsSold = document.forms[0].contractsSold.value;
var priceSold = document.forms[0].priceSold.value;
var totalAdded = (contractsAdded * priceAdded) * 100;
var totalSold = (contractsSold * priceSold) * 100;
var gainLoss = totalSold - totalAdded;
var percent = gainLoss / totalAdded;

document.forms[0].totalAddedCost.value = totalAdded;
document.forms[0].totalSoldCost.value = totalSold;
document.forms[0].gainLoss.value = gainLoss;
document.forms[0].returnPercent.value = percent;
}

View 4 Replies


ADVERTISEMENT

Function Only Runs 33 Percent Of Time When Called?

Feb 23, 2010

function update(value1){
doAjax("behind_scan.php" , "id="+value1);
}function doAjax(url , str ){
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null){
alert ("Browser does not support HTTP Request");
return;
}url=url+"?"+str;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}function stateChanged(){
if (xmlhttp.readyState==4){
//document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
//alert(xmlhttp.responseText)
document.forms.myform.scanner.value = "";
document.forms.myform.scanner.focus();
}}function GetXmlHttpObject(){
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}if (window.ActiveXObject){
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
} return null;
}

This pages calls a function on a separate php page to run 2 mySQL queries, the value must be put in the text 3 times in quick succession before the database is altered. I've tried all the other functions and they seem to be fine, AJAX is my weakness but I need it for the main functionality of my website. I need this to work.

View 2 Replies View Related

Calculating Shipping With If/else And Function?

Sep 21, 2009

I am having difficulty getting the following assignment to run properly:Many companies charge a shipping and handling charge for purchases. Create a Web page that allows a user to enter a purchase price into a text box and includes a JavaScript function that calculates shipping and handling. Add functionality to the script that adds a minimum shipping and handling charge of $1.50 for any purchase that is less than or equal to $25.00.For any orders over $25.00, add 10% to the total purchase price for shipping and handling,but do not include the $1.50 minmum shipping and handling charge. The formula for calculating a percentage is price * percent / 100. For example, the formula for calculating 10% of a $50.00 purchase price is 50 * 10 / 100, which results in a shipping and handling charge of $5.00. After you determine the total cost of the order (purchase plus shipping and handling), display it in an alert dialog box. Save the document as CalcShipping.html.This is what I have after working on it round and round for 4 hours yesterday:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">[code].....

View 5 Replies View Related

Populating Layer With Function?

Dec 23, 2009

I need to populate a layer with multi line text. My code below works ok on the document. write to the layer when the function is fired.

<script type="text/javascript">
function insertText(){
var mytext = "The Text to be shown in layer1!

[Code]....

View 2 Replies View Related

Calculating Input Fields - Get The Math Function To Calculate And Display Properly

Nov 5, 2009

I can't seem to get the math function to calculate and display properly. Can anyone point me in the right direction.

Here is my code.

View 2 Replies View Related

Populating An HTML Select From A JavaScript Function

Dec 12, 2005

How would I go about simply populating an HTML select statement from a JavaScript function. You see I have several HTML select statements on one page which are identical (sometimes repeated multiple times) so I wanted to try and cut down on these multiple instances and just have the values hard coded once (in a JavaScript function perhaps) and then called whenever they are needed. Could anyone advice me please, would this be easy to do?

I have the following HTML select statement hard coded:

<select name="bodyFontType">
<option value="">Select Font</option>
<option value="Verdana">Verdana</option>
<option value="Arial">Arial</option>
<option value="Tahoma">Tahoma</option>
<option value="Trebuchet MS">Trebuchet MS</option>
</select>

Would I use an onLoad property on the select statement to call a function which creates an array and then pushes through the values?

View 2 Replies View Related

JQuery :: Click Function Populating Value In A Hidden Field?

Oct 1, 2009

I have a table set up with a .click() function on each row sothat if it is clicked on, a class is added to the row thus changingthe background, etc. I was wondering if there was is a way to takethe id of the row that was clicked and populate a hidden field withit. What would I need to include in my .click() function for this towork?Here is my current one:

$("tr").click(function () {
$(".clicked").removeClass("clicked");
$(this).addClass("clicked");

[code]....

View 2 Replies View Related

Negative Percent Work Around?

Jun 9, 2010

I have the following calculation (simplified to focus on the point):

Code:
var scroll_percentage = scroll_button_distance/scroll_wrapper_width;
or imagine this is:

Code:
var scroll_percentage = 100/500;
The answer is -INFINITY

How do I make this into a usable number?I searched for INFINITY and found plenty of info but am not sure how to simply get the "0.2" answer I need for the next calculations.

View 3 Replies View Related

JQuery :: Changing Function From Populating With Images From Text File

Jul 2, 2010

I'm only new to Jquery and Ajax for that matter, understand JavaScript but don't work with it everyday. I have a function from Jcarousel that is populating a div with images (talking to a .txt file which contains the url), JavaScript then writes out the image.
function getItemHTML(data){
var split = data.split(";");
var url = jQuery.trim(split[0]);
var title = jQuery.trim(split[1]);
var url_m = url.replace(/_s.jpg/g, '_m.jpg');
return '<a href="' + url_m + '" title="' + title + '"
class="thickbox"><img src="' + url + '" width="' + 75 + '
" height="' + 75 + '" alt="' + title + '" /></a>';
};

And this is the function talking to the datasource
function loadItemHandler(carousel, start, last, available){
if (available) {
// Trigger loaded
carousel.loaded();
return;
}var cr = carousel;
jQuery.get("example_dynamic_ajax.txt", function(data) {
appendItemCallback(cr, start, last, data);
});};
Now what I would like to do is instead of populating with images from .txt file I would like to populate with code from another .html page (a html page containing a table code). I'm just not entirely sure what object I need to use and how to go about that. Full code that I am working with is here: [URL].

View 2 Replies View Related

JQuery :: Mask Plugin Percent Field?

Apr 1, 2010

I'm using this plugin:I was wondering if anybody knew of a way to place a percent mask on one input field that can have the possibility to be in both of these formats..

field
<input type="text" id="interest_rate" />
mask

[code]...

View 1 Replies View Related

Script Not Telling Difference Between Percent Signs And Asterisks / Why Is So?

Mar 25, 2010

I am using Javascript to generate a SQL query. When querying, I DO want asterisks to serve as a wildcard, but do NOT want percent signs to do so.

So, I have some code like this code...

But the above replaces asterisks with "!%" also! That's exactly what I don't want to have happen!

Does anyone know what I am missing here?

View 1 Replies View Related

Onclick Subtraction - Using A Percent Bar To Redirect The Page When All Buttons Are Clicked

Jan 10, 2010

right now im using a percent bar to redirect the page when all buttons are clicked but i want a more efficient way of doing the redirect there will be a total number of buttons on my page, for examply 5 every time one is clicked, is there a possible way of subtracting 1 and when buttons = 0 the page redirects

View 1 Replies View Related

Declare A Variable Inside A Function - Returns White Space - Not Variable Value

Aug 17, 2010

I am trying to declare a variable inside a function and use it later on in my code... but it just already returns white space... i.e. not variable value. I am setting it within this function:

function show_video1(){
document.getElementById('video1').style.display="block";
var video1Name = "Education World News (Part 1)";
document.getElementById('video2').style.display="none";
document.getElementById('video3').style.display="none";
document.getElementById('video4').style.display="none";
[Code]...

and trying to call it later on with this: <script type="text/javascript">document.write(video1Name)</script> It might be worth noting that each one of my 11 videos will hace a different name.

View 13 Replies View Related

Scroll Function Without Scrollbar Showing?

Oct 25, 2010

Here is the page I'm working on: [URL].. As you will see there are only two pictures on there now. I want the left table cell to have an up arrow and a down arrow below those pictures where the user can scroll (down and up) and see other pictures, which when clicked on will show a larger image to the right. It's setup now as you will see for the larger image to appear to the right. It's just not setup for this scroll function where the user can click on an up arrow or down arrow to make the users see the other pictures in the table cell. Also this section should not have a scrollbar show up when the pictures are being scrolled down to see. I'm not sure on how to proceed with this.

View 3 Replies View Related

Jquery :: Function Works Fine But Form Showing Failed Message

Sep 19, 2009

I have a simple form that inserts a name into a database. The functionality works fine. But the message that is suppose to show is wrong.

Code JavaScript:
$(document).ready(function () {
$('#nameForm').submit(function () {
$('.msgbox').hide();
$.post('ajax.Register.php', {name: $('#name').val()}, function (data) {
if (data) {
$('.msgbox').show('fast', function () {
$(this).html('OK!');
});
}else {
$('.msgbox').show('fast', function () {
$(this).html('Failed!');
});
}});
return false;
});
});

The form always shows "Failed!" even though the name went through in my database.
Code PHP:
try {
$objUser = new PDO("mysql:dbname=ajax;host=localhost", "root", "");
}
catch (PDOException $e) {
echo 'Unable to connect';
}
$strName = $_POST['name'];
if (empty($strName))
return false;
 
$strQuery = "INSERT INTO name (name) VALUES (:name)";
$objStatement = $objUser->prepare($strQuery);
$objStatement->bindParam(':name', $strName, PDO::PARAM_STR);
if ($objStatement->execute())
return true;
return false;

View 5 Replies View Related

JQuery :: Use A Variable Declared In A Function Outside A Function?

Nov 17, 2011

So I've got this checkbox thing going on, where an array converted with the value of each checkbox in it.It looks like this:

[Code]...

What I want out of this function is the variable newworkdays to use in another function.

How do I access the "newworkdays" outside the function? I've already try to declare the variable outside of the function, though that resulted in that I didn't get the value of the variable inside the function.

View 1 Replies View Related

Passing Variable To A Function Inside A Function?

Feb 10, 2010

Here is the code:

for (var i = 0; i < BS_crm['activityTypes'].length; i++) {
var clickFunc = function(){ activityList.showForm( -1, {blockType:[""+BS_crm['activityTypes'][i]['id'], "0"]} ); };
var type = {

[Code]....

Now, basically what I am doing here is running through one array to create an array of objects, that will be used to create links that will use whatever onClick function I pass it. The problem is that on the second line I need the BS_crm['activityTypes'][i]['id'] to be a value, not a reference. If that line was simply changed to:

var clickFunc = function(){ activityList.showForm( -1, {blockType:["3", "0"]} ); };

View 4 Replies View Related

Accessing A Variable Defined In A Function, Outside The Function?

Sep 18, 2010

I'm making some changes to a google chrome extension I made and am having some trouble. Heres my code on a content script page (removeAttr.js) :

chrome.extension.sendRequest({greeting: "whitelist"}, function(response) {
var whitelist = response.whitelist;
console.log(response.whitelist);//working
});
alert(whitelist);//alerts "undefined"

How do I acess the whitelist variable from outside the sendrequest() function?

Iv tried saving it to a window.var variable with no luck. Iv tried creating a div and assigning it's innerHTML as the whitelist variable and getting it later with no luck. The fact that it's a chrome extension complicates things because i dont actually know if i can create elements from where the script is located.

View 7 Replies View Related

Pass Variable From Function To Another Function?

Mar 21, 2010

how to pass variable say(n) value to another function...here below my script:

<html>
<head>
<script type="text/javascript">
function docalc(){

[Code].....

View 5 Replies View Related

Adding Up, Calculating Value?

Mar 1, 2010

i have a table, with 2 numeric values at the minute, if the checkbox is selected I want the total price to be shown? this is the form I have with the numeric values

<form id="calculation" action="#" method="post">
100 <input type="checkbox" name="check1" value="100" onClick='total_cost()'/>
120 <input type="checkbox" name="check2" value="200" onClick='total_cost()'/>[code].....

View 5 Replies View Related

Calculating A Person's Age?

Dec 1, 2010

I am trying to write a script that will take a date of birth and another date and, if the person was over 75 years old on the createdDate, return "true" (otherwise return "false").

Unfortunately my code doesn't seem to return either 'true' or 'false'!

The code is:

var dateofBirth="30/10/1982";
var createdDate="01/12/2010";
var sd = dateofBirth.split('/');
var dob = new Date(sd[2],sd[1],sd[0]);

[Code]....

View 2 Replies View Related

Use A Function With More Than One Variable?

Feb 18, 2009

I am currently working on a code that has a function within it called findHighestValueIndex which will find the highest value in an array. The problem I am having is writing this function so i can call it in the program to work on 2 different arrays. I already have it working on the array assesmentScoresArray, but I also need it to work on the weightedScoresArray. Is it actually possible to do this or do i need to write another function that is almost the same but with the arguments etc for the other array? I will paste the code below so you can see where I am up to. I am a novice in programming so im probably going about this all the wrong way

<HTML>
<HEAD>
<TITLE>

[code]....

View 4 Replies View Related

Set The Value Of A Variable With A Function ?

Aug 15, 2010

Im trying to set the value of a variable with a function, the variable i want changed is defined in another variable.

Then later i have this:

The "Selected" var is the var i want to change, and the value of it will change every time one is changed (it's for a calculator) When i try it though, it changes the value of the "selected" var it'self and not the var that i want changed. how i can i got it to change the value of the value of selected and not the selected var it'self?

View 2 Replies View Related

Getting The Value Of A Variable From Another Function.

Jun 1, 2007

Heres what i got, ignore all the unknowns they come from textboxes etc.

My problem is in this function I change the value of a variable. I want to store that variable change so i can use it in another function. Heres what i got.


function setupGame(){
var guessesLeft = guessNo.value = 10;
var wordSplice = new Array();
wordVal = word.value
wordVal = wordVal.toLowerCase();
alert(wordVal);
for(var i=0; i<wordVal.length; i++){
wordSplice[i] = wordVal.substring(i, i+1);
wordSoFar.value += "*"
}}

Thats the first function, the variable or 'Array' values of wordSplice I saved I want to use in this function :

function runCoreEngine(){
for(i in wordSplice){
}
}

View 2 Replies View Related

Using A Variable From Url In Function

Jul 11, 2011

How I would modify the following function to get it to check if a url variable is present and if so to add it into the string variable for the url.

Code:

If my url is index.php?make=Apolo then I need to get this added into the var queryString part of the function so that the ajax returns the correct values.

View 1 Replies View Related

Add Php Variable To Url In Function?

Apr 22, 2011

I'm trying to add a php variable to a url in the following function but can't get it to work...

Code:
<?php
$leEcho = "theEcho";
?>

[Code].....

View 11 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved