0

This is supposed to calculate circumference, however, I am only getting a zero returned. What am I doing wrong?

<html>
<head>
<script type="text/javascript">
    var index = false;
    var text = "This text shifts";

var Pi = 3.14159265;
    var dia = document.getElementById("txtdia");  
var circumf =  dia * Pi;

    function DisplayText(){

        document.getElementById("txtcircumf").value = circumf;
    }
</script>
</head>
<body>
<form> 
   <input type="text" id="txt1"/>
   <input type="text" id="txt2"/><br>
   <input type="text" name="txtdia" />
   <input type="text" name="txtcircumf" />  
   <input type="button" value="Change Text" onclick="DisplayText()"/>
</form>
</body>
</html>
2
  • 1
    Instead of var Pi = ...; you could just use the Math object's constants -- Math.PI Commented Jan 2, 2014 at 17:04
  • You forgot to give id attribute in txtcircumf Commented Jan 2, 2014 at 17:04

3 Answers 3

2

The primary problem you have is that this script will run prior to your dom being ready. As a result, even if you were properly grabbing the diameter's value it still wouldn't work, since document.getElementById("txtdia") wouldn't return anything.

I would just fetch the diameter's value each time.

function DisplayText(){
    var dia = document.getElementById("txtdia").value;  
    var circumf =  dia * Pi;
    document.getElementById("txtcircumf").value = circumf;
}

The other option of course is to put this entire script after your html. Ie

</body>

<script type="text/javascript">
    var index = false;
    var text = "This text shifts";
Sign up to request clarification or add additional context in comments.

3 Comments

Fantastic, just learning javascript. not sure how you got this question and then wrote the answer so fast, within 3 minutes. Crazy FAST! anyway Thanks so much, it works perfect.
@R2Builder - glad to help. Be sure to accept the best answer by clicking the checkmark next to it. (The best answer, which definitely may not be mine, given what others have posted)
Ok I see it now checked green
2

There are 3 distinct issues which you need to fix for this to work correctly.

  1. txtcircumf and textdia are the name of the elements, not the id, so using document.getElementById will fail.

    Fix: Add that as an id onto the elements in question:

    <input type="text" name="txtdia" id="txtdia" />
    <input type="text" name="txtcircumf" id="txtcircumf" />  
    
  2. The elements are not present when the script first runs. This is the issue described by @AdamRakis and his fix is probably best - always retrieve the value when you need it:

    function DisplayText(){
        var dia = document.getElementById("txtdia").value;  
        var circumf =  dia * Pi;
        document.getElementById("txtcircumf").value = circumf;
    }
    
  3. A minor point, but when you read the .value of a field you get text, as you are doing a mathematical equation it is common practice to ensure the value you're wouking with is numeric. You can use parseFloat for this:

    function DisplayText(){
        var dia = parseFloat(document.getElementById("txtdia").value);  
        var circumf =  dia * Pi;
        document.getElementById("txtcircumf").value = circumf;
    }
    

Comments

0

I made a couple structural changes that improve the overall quality of your code :) (see the arrows for changes)

<html>
<body>
<form> 
   <input type="text" id="txt1"/>
   <input type="text" id="txt2"/><br>
   <input type="text" name="txtdia" />
   <input type="text" name="txtcircumf" />  
   <input type="button" id="derp" value="Change Text" /> //<-- added ID, removed inline JS
</form>

<script type="text/javascript">
document.getElementById("derp").onclick = function() { //<-- use this style instead of inline onclicks!
    var index = false;
    var text = "This text shifts";

    var dia = document.getElementById("txtdia").value; //<-- .value, not the whole element!  
    var circumf =  dia * Math.PI; //<-- Math.PI is an object constant, very handy
    document.getElementById("txtcircumf").value = circumf;
    //no more standard function!
}
</script>

</body>
</html>

5 Comments

Thanks, but the above code causes Error in IE 9 >> Unable to set value of the property 'onclick': object is null or undefined on line 4
What error would that be? I forgot since you're doing math on an input you'll have to parsefloat the .value
Math.PI must need a library or somehting, when I use it I get "NaN" for an answer on stead of a cricumference. also error on line 4
No library needed sir, like I said you need to parsefloat the element value, else you'll be doing math with a string with will yield NaN (not a number)
thanks all... I tried each one but the first one does the trick.

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.