最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript object is not a function - Stack Overflow

programmeradmin0浏览0评论

I get the following error and I can't find why:

TypeError: object is not a function

Here is my html:

<td><input type="text" value="" id="cash_retained" name="cash_retained" onkeyup="net_cash()" size="25"></td>
<td><input type="text" value="" id="cash_change" name="cash_change" onkeyup="net_cash()" size="25"></td>
<td><input type="text" value="0" id="net_cash" name="net_cash"></td>

Here is my js function:

function net_cash() {
    var cash = 0;
    var retained = document.getElementById("cash_retained");
    var change = document.getElementById("cash_change");

    cash += parseInt(retained.value);
    cash += parseInt(change.value);

    if (isNaN(cash)) {
        document.getElementById("net_cash").value = "0";
    } else {
        document.getElementById("net_cash").value = cash;
    }
}

I can't see for the life of me why this is not working. I have other similar js functions that are finding it just fine.

I get the following error and I can't find why:

TypeError: object is not a function

Here is my html:

<td><input type="text" value="" id="cash_retained" name="cash_retained" onkeyup="net_cash()" size="25"></td>
<td><input type="text" value="" id="cash_change" name="cash_change" onkeyup="net_cash()" size="25"></td>
<td><input type="text" value="0" id="net_cash" name="net_cash"></td>

Here is my js function:

function net_cash() {
    var cash = 0;
    var retained = document.getElementById("cash_retained");
    var change = document.getElementById("cash_change");

    cash += parseInt(retained.value);
    cash += parseInt(change.value);

    if (isNaN(cash)) {
        document.getElementById("net_cash").value = "0";
    } else {
        document.getElementById("net_cash").value = cash;
    }
}

I can't see for the life of me why this is not working. I have other similar js functions that are finding it just fine.

Share Improve this question edited Jan 20, 2013 at 9:51 j0k 22.8k28 gold badges81 silver badges90 bronze badges asked Jan 20, 2013 at 9:47 Amy NevilleAmy Neville 10.6k13 gold badges61 silver badges98 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

It appears the problem here is that you have a form element with the same name as your function:

<td><input type="text" value="0" id="net_cash" name="net_cash"></td>

So when you call net_cash() in your onkeyup event, it thinks you are referring to this input DOM object rather than the function of the same name. I suggest ing up with separate names for these two things.

Inline event handlers have the enclosing <form> element as their executing scope. Each form control with a name attribute is treated as though it's a variable in that scope and that's where the conflict es from.

This is also yet another great reason to use unobtrusive JavaScript instead of inline event handlers. If you do that, you don't have to worry about your function names conflicting with your element names. See onclick=“” vs event handler for more information on the pitfalls of using inline event handlers.

Your code is working fine for me. See demo here

So make sure of the following

  1. Make sure that you are including the script before calling the function.
  2. Also, it is not a good idea to keep the ids/names and function names same

    <td><input type="text" value="0" id="net_cash" name="net_cash"></td>
    

    // Change the idand name here

发布评论

评论列表(0)

  1. 暂无评论