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

javascript - TypeError: Functionname is not a function at HTMLButtonElement.onClick - Stack Overflow

programmeradmin0浏览0评论

I'm pretty new to Javascript and playing around with it at the moment. However, I can't actually test my code because I get the following error:

Uncaught TypeError: namecaller is not a function
at HTMLButtonElement.onclick (Tools.html:101)

Here is my code:

div id="content">
    <script>
        function namecaller(){
            var a = "scurvy";
            var b = "dog";
            document.getElementById("namecaller").innerHTML = "You are a " + a + " " + b;
        }
    </script>
Namecaller</p>
<button type="button" onclick="namecaller()">
You are a...</button>

I have no clue why it doesn't work, looked at other StackOverflow questions and also at the W3 tutorials.

I'm pretty new to Javascript and playing around with it at the moment. However, I can't actually test my code because I get the following error:

Uncaught TypeError: namecaller is not a function
at HTMLButtonElement.onclick (Tools.html:101)

Here is my code:

div id="content">
    <script>
        function namecaller(){
            var a = "scurvy";
            var b = "dog";
            document.getElementById("namecaller").innerHTML = "You are a " + a + " " + b;
        }
    </script>
Namecaller</p>
<button type="button" onclick="namecaller()">
You are a...</button>

I have no clue why it doesn't work, looked at other StackOverflow questions and also at the W3 tutorials.

Share Improve this question edited Jul 11, 2018 at 11:42 Bharata 14.2k6 gold badges43 silver badges53 bronze badges asked Jul 11, 2018 at 9:53 JasperMWJasperMW 5333 gold badges7 silver badges24 bronze badges 1
  • where is element with id namecaller ? – Muhammad Usman Commented Jul 11, 2018 at 9:56
Add a comment  | 

5 Answers 5

Reset to default 7

function namecaller() {
  var a = "scurvy";
  var b = "dog";
  document.getElementById("content").innerHTML = "You are a " + a + " " + b;
}
<div id="content"></div>
<button type="button" onclick="namecaller()">
    You are a...</button>

The code seems to have a lot of problems, like your tags were not properly written. The tags need to be opened and closed properly. And your script should be in the right place so that it gets compiled. Then you have not given an ID to any of the elements, and try calling them. So here is a working example of how the code should be properly written for bug-free compilation.

Sometimes, it is because your function name override some other functions, especially the system defined ones. For example, I once made a function named as "translate." Later, it reports the exactly same error like yours. The solution is quite simple: just change the function name. For me, I make it "function tranlateIt()" and hence the problem is gone forever.

This is not the solution to your particular problem (the accepted answer answers it neatly), but the following might be the useful reference to the future readers (it happened very often to me).

The same error you provided happened to me, when I first declared a variable and then the function with the same name. For example:

var example = 5;

function example() {
  console.log("The function just got executed!");
}
<button onclick="example()">Click me!</button>

You have not defined the id of paragraph for which you are changing the value. Just define the Id of paragraph as namecaller and it will work. See the code below:

<div id="content">
<script>
    function namecaller(){
        var a = "scurvy";
        var b = "dog";
        document.getElementById("namecaller").innerHTML = "You are a " + a + " " + b;
    }
</script> 
<p id = "namecaller">Namecaller</p>
<button type="button" onclick="namecaller()">
You are a...</button

// Write this function code in inside <script> tag or in index.js file
function namecaller() {
  var a = "scurvy";
  var b = "dog";
  document.getElementById("content").innerHTML = "You are a " + a + " " + b;
}
.content{
  background-color: bisque;
}
.button{
  color: white;
  background-color: blue;
}
<!-- Write this code in inside the <body> tag in your index.html file -->
<!-- I added little CSS -->
<div class="content" id="content"></div>
<button class="button" type="button" onclick="namecaller()">You are a...</button>

Your div tag is not properly written. all tags need to open and close properly. You have to give the proper id to any HTML element and then call the function. Run the code snippet.

发布评论

评论列表(0)

  1. 暂无评论