0

I have the following script where a variable gets its value from an input field, however when I run my function its not working, returns nothing. Im new to JS so im unsure if it needs to be part of a function *even though Ive tried this with no luck) or what...

/////////////////////////////////////////////////////////////////////

// Variables

// Content/SLA
var ContentMinutes = '';
var ContentMinutesSelector; // Switch Case
var ServiceLevel = 5;
var NoOfFrames = 2;

// Render Time (Hairier the Better)
var AvgFrameRenderTime = '';
var AvgFrameRenderTimeSelector = 10; // Switch Case
var CoresInTest = document.getElementById('CoresInTest').value;

// Other
var EstimatedCoreHours = NoOfFrames * CoresInTest * AvgFrameRenderTimeSelector;

// Cost Estimate
var CostEstimate = ServiceLevel * EstimatedCoreHours;


/////////////////////////////////////////////////////////////////////

//  Functions

function CalculateEstimate() {  
// Estimate Cost
parseInt(document.getElementById("PriceEstimate").innerHTML=CostEstimate.toFixed(2));

// Estimate Core Hours
parseInt(document.getElementById("EstimatedCoreHours").innerHTML=EstimatedCoreHours.toFixed(    2));
}

my PriceEstimate and EstimatedCoreHours fields are both just empty divs, <div id="EstimatedCoreHours"></div>, My calculations work if i define a value for the variable as opposed to document.getElementById so I believe I must need to run a function or something to update all the vartiables?

But if I set...

var CoresInTest = document.getElementById('CoresInTest').value;

to

var CoresInTest = 10;

Then it works fine...

Its not actually my return, the problem is my variables arent calling, IF i define them with a number then it works.

2
  • 1
    If PriceEstimate or EstimatedCoreHours are input fields (<input type="text"/>), you should use .value and not .innerHTML. Commented May 24, 2011 at 7:43
  • Something I noted, you're calling parseInt, but you're not taking the value it returns. And your method doesn't actually have any return statements, so it's not going to return anything. Commented May 24, 2011 at 7:46

3 Answers 3

1

I guess you need to do something like this, if you are looking to get calculated data in your div.

document.getElementById("PriceEstimate").innerHTML=parseInt(CostEstimate.toFixed(2));    
// Estimate Core Hours
document.getElementById("EstimatedCoreHours").innerHTML=parseInt(EstimatedCoreHours.toFixed(2));

If var CoresInTest = 10; works fine, then your code is placed wrong.

What element is CoresInTest? is it a text field? and if so is this script placed or called before the element renders? then you will have to reinitialize that variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Not sure what that changes. Not sure why the author has 'parseInt' and 'toFixed' together. The requirement is either integer or 2 decimal places, I'm guessing it's 2 decimal places.
0

If PriceEstimate and EstimatedCoreHours are elements you should use the value property this might work for you:

document.getElementById("PriceEstimate").value=parseInt(CostEstimate.toFixed(2),10);    
document.getElementById("EstimatedCoreHours").value=parseInt(EstimatedCoreHours.toFixed(2),10);

1 Comment

Question says they're DIV elements. Not form fields.
0

If var CoresInTest = 10; makes it work fine, then it must be something to do with document.getElementById('CoresInTest').value - so why isn't that working? Is it a drop down list? Instead of us guessing, tell us.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.