0

Why am I getting this error Failed to execute 'createComment' on 'Document': 1 argument required, but only 0 present?, if my function is not taking in any arguments?

      <form onsubmit='createComment()' method='POST'>
var createComment = function () {

var author = $('#addCommentAuthor').val()
var email = $('#addCommentEmail').val()
var content = $('#addCommentContent').val()
var date = $('#addCommentDate').val()
$.ajax({
url: "controller.php",
type: 'POST',
data: {
  commentType: commentType,
  questionId: currentQuestionID,
  add_comment_author: author,
  add_comment_email: email,
  add_comment_content: content,
  add_comment_date: date
},
success: function(data) {
  console.log(data)
}
})
}
runucegop
  • 93
  • 1
  • 2
  • 9
  • 1
    `createComment` already exists on `document`. When placing a function call into the `onsubmit` handler, it’ll actually call `document.createComment`. To fix this, rename your function to something else, or use the standard `addEventListener`. – Sebastian Simon Sep 07 '17 at 12:55
  • Possible duplicate of [Uncaught TypeError: lang is not a function](https://stackoverflow.com/questions/38276407/uncaught-typeerror-lang-is-not-a-function) — replace `lang` by `createComment` and it’ll be exactly the same problem (also, `animate`, if you follow the duplicate chain). – Sebastian Simon Sep 07 '17 at 12:57
  • Isn't it erased everytime I update it? – runucegop Sep 07 '17 at 12:58
  • What is erased? – Sebastian Simon Sep 07 '17 at 12:59
  • I noticed that all your three questions are the same problem. – Sebastian Simon Sep 07 '17 at 13:00

1 Answers1

5

I had a similar problem, in my HTML I had given a button an onclick attribute and the attributes value was a function that I had defined in my JavaScript file.

I had named my function: createElement()

<button onclick ="createElement()"> </button>

However when I clicked it, my function wasn't executing, instead was getting the error message:

index.html:28 Uncaught TypeError: 
Failed to execute 'createElement' on 'Document': 
1 argument required, but only 0 present.
    
at HTMLButtonElement.onclick

What I hadn't appreciated was that, my chosen function name was clashing with the pre-existing JS method named:

document.createElement() 

which is used like this:

document.createElement("div")

So presumably, my HTML was trying to invoke this native JS method, instead of my own

After discovering this, I simply renamed my function, and everything worked like normal again:

 <button onclick ="createEl()"> </button>
user13764383
  • 53
  • 1
  • 4