13

I am having trouble understanding why this is not working. I have two fields on my form and when I click a button another text field value is changed to that if the function. How can I get this to work?

function calculate()
{
    var odometerStart = parseFloat (document.getElementById('odometerStart').value);
    var odometerEnd = parseFloat(document.getElementById('odometerEnd').value);
    var distance = document.getElementById('distance');
    var amount = document.getElementById('amount');

    distance.value = odometerEnd - odometerStart;       
}

var val = $("#taskentry").validate({
    rules: {
        tripDate: {required: true}
        tripContact: {required: true}
        odometerStart: {required: true}     
    }
});


Odometer: Start <input type="number" name="odometer[Start]" id="odometerStart" min="0" max="999999" placeholder="0" class="required"/><br/>
Odometer: End <input type="number" name="odometer[End]" id="odometerEnd" min="0" max="999999" placeholder="999999" class="required"/><br/>
Comments <textarea name="comments" id="comments" rows="2" cols="2"></textarea><br/>
Distance <input type="text" name="distance" id="distance" value="" placeholder="0.00"/><br/>
<input type="button" name="calculate" value="Calculate" onclick="calculate()"/>

I am debuging this in Google Chrome using the developer tools and am getting the error "uncaught TypeError: Object is not a function" at the point where the calculate button is.

1
  • Your code works. Here's a fiddle to prove it: nothing on JSLint and no exceptions returned -- jsfiddle.net/YahzF Commented Oct 14, 2011 at 22:27

4 Answers 4

55

Sorry for the huge gap between your question and my answer :) I just got the same problem and found the reason - browser automatically populates objects based on ID attributes of the html tags in the page. Simple test

<div id='test'>Just testing</div>
<script type='text/javascript'>
alert(test.innerHTML);
</script> 

In your case you had an element with id='calculate', rename it or rename the function you are using for calculations.

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

2 Comments

Had the same issue and this was my fix
I didn't know about this browser behaviour, until just now! I wouldn't need a lot of document.getElementById() if I knew about this.
7

In my case, I have

<input type="button" class="button" id="register" title="register" value="Sign Up!" onclick="register()" />

and get the same warning when I call the javascript function register(), I guess it is conflicted with id or title attributes in the input tag, so I just alter this function into another name such as signup() and it works around.

I hope it helps. :)

Comments

4

I had this problem with a function I created, working perfectly with Firefox and giving this error with Chrome and Safari. The solution: change the name of the function. The original name was expand(target), replaced by expand_compress(target). And it worked. I suppose that the function expand() is existing somewhere else than in Firefox

1 Comment

You might want to take note of Cheery's answer as to why that change matters. You probably had an html tag that had an id of "expand"
-4

Perhaps you need commas:

var val = $("#taskentry").validate({
    rules: {
        tripDate: {required: true},
        tripContact: {required: true},
        odometerStart: {required: true}     
    }
});

2 Comments

He was saying you had a syntax error. Commas separate properties of an object, so keep those commas there.
why is this marked as the correct answer if the asker says it didn't work?

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.