admin管理员组文章数量:1023815
I'm having issues with JavaScript not detecting the click event on a button. Here is my HTML code:
<input type="button" value="Upload"
id="filemanagement_uploadbutton[0]"
name="filemanagement_uploadbutton"
onclick="javascript:uploadFiles();"
>
Here is my JavaScript code (with console.log commands for error checking):
let fileButtons = document.getElementsByName('filemanagement_uploadbutton');
console.log(fileButtons.length); // returns 2, since I have two buttons
console.log(fileButtons[0].id); // returns filemanagement_uploadbutton[0]
console.log(fileButtons[0].type); // returns button
console.log(fileButtons[0].click); // returns javascript:uploadFiles();
fileButtons[0].click();
I've also tried:
let fileButton = document.getElementsByName('filemanagement_uploadbutton')[0];
fileButton.click();
Both of these result in the error:
Uncaught TypeError: fileButtons[0].click is not a function
or
Uncaught TypeError: fileButton.click is not a function
How is it telling me that click is not a function on the element that it's telling me the click function is javascript:uploadFiles();
literally one element above?
Edit: I've also tried these variations:
let fileButtons = document.getElementsByName('filemanagement_uploadbutton')[0]; //obviously the command for fileButtons[0].length won't work here as expected, so I comment it out
let fileButton = document.getElementById('filemanagement_uploadbutton[0]');
with the appropriate changes to the code below when I reference them.
I'm having issues with JavaScript not detecting the click event on a button. Here is my HTML code:
<input type="button" value="Upload"
id="filemanagement_uploadbutton[0]"
name="filemanagement_uploadbutton"
onclick="javascript:uploadFiles();"
>
Here is my JavaScript code (with console.log commands for error checking):
let fileButtons = document.getElementsByName('filemanagement_uploadbutton');
console.log(fileButtons.length); // returns 2, since I have two buttons
console.log(fileButtons[0].id); // returns filemanagement_uploadbutton[0]
console.log(fileButtons[0].type); // returns button
console.log(fileButtons[0].click); // returns javascript:uploadFiles();
fileButtons[0].click();
I've also tried:
let fileButton = document.getElementsByName('filemanagement_uploadbutton')[0];
fileButton.click();
Both of these result in the error:
Uncaught TypeError: fileButtons[0].click is not a function
or
Uncaught TypeError: fileButton.click is not a function
How is it telling me that click is not a function on the element that it's telling me the click function is javascript:uploadFiles();
literally one element above?
Edit: I've also tried these variations:
let fileButtons = document.getElementsByName('filemanagement_uploadbutton')[0]; //obviously the command for fileButtons[0].length won't work here as expected, so I comment it out
let fileButton = document.getElementById('filemanagement_uploadbutton[0]');
with the appropriate changes to the code below when I reference them.
Share Improve this question edited Nov 18, 2024 at 19:06 Steve G. asked Nov 18, 2024 at 17:46 Steve G.Steve G. 4092 gold badges10 silver badges24 bronze badges 8 | Show 3 more comments2 Answers
Reset to default 0Instead of using the inline HTML "onclick" function, you could try removing it and using an event listener in your JavaScript instead.
let fileButtons = document.getElementByName('filemanagement_uploadbutton');
if (fileButtons.length > 0) {
fileButtons[0].addEventListener('click', uploadFiles);
fileButtons[0].click();
}
You are need to have your code something like this.
First you need get the element by id using document.getElementById('filemanagement_uploadbutton[0]')
and then add the add event listener.
addEventListener('click', () => {
// your code goes here
});
document.getElementById('filemanagement_uploadbutton[0]').addEventListener('click', () => {
alert('hi');
});
<button id="filemanagement_uploadbutton[0]" name="filemanagement_uploadbutton">Upload</button>
I'm having issues with JavaScript not detecting the click event on a button. Here is my HTML code:
<input type="button" value="Upload"
id="filemanagement_uploadbutton[0]"
name="filemanagement_uploadbutton"
onclick="javascript:uploadFiles();"
>
Here is my JavaScript code (with console.log commands for error checking):
let fileButtons = document.getElementsByName('filemanagement_uploadbutton');
console.log(fileButtons.length); // returns 2, since I have two buttons
console.log(fileButtons[0].id); // returns filemanagement_uploadbutton[0]
console.log(fileButtons[0].type); // returns button
console.log(fileButtons[0].click); // returns javascript:uploadFiles();
fileButtons[0].click();
I've also tried:
let fileButton = document.getElementsByName('filemanagement_uploadbutton')[0];
fileButton.click();
Both of these result in the error:
Uncaught TypeError: fileButtons[0].click is not a function
or
Uncaught TypeError: fileButton.click is not a function
How is it telling me that click is not a function on the element that it's telling me the click function is javascript:uploadFiles();
literally one element above?
Edit: I've also tried these variations:
let fileButtons = document.getElementsByName('filemanagement_uploadbutton')[0]; //obviously the command for fileButtons[0].length won't work here as expected, so I comment it out
let fileButton = document.getElementById('filemanagement_uploadbutton[0]');
with the appropriate changes to the code below when I reference them.
I'm having issues with JavaScript not detecting the click event on a button. Here is my HTML code:
<input type="button" value="Upload"
id="filemanagement_uploadbutton[0]"
name="filemanagement_uploadbutton"
onclick="javascript:uploadFiles();"
>
Here is my JavaScript code (with console.log commands for error checking):
let fileButtons = document.getElementsByName('filemanagement_uploadbutton');
console.log(fileButtons.length); // returns 2, since I have two buttons
console.log(fileButtons[0].id); // returns filemanagement_uploadbutton[0]
console.log(fileButtons[0].type); // returns button
console.log(fileButtons[0].click); // returns javascript:uploadFiles();
fileButtons[0].click();
I've also tried:
let fileButton = document.getElementsByName('filemanagement_uploadbutton')[0];
fileButton.click();
Both of these result in the error:
Uncaught TypeError: fileButtons[0].click is not a function
or
Uncaught TypeError: fileButton.click is not a function
How is it telling me that click is not a function on the element that it's telling me the click function is javascript:uploadFiles();
literally one element above?
Edit: I've also tried these variations:
let fileButtons = document.getElementsByName('filemanagement_uploadbutton')[0]; //obviously the command for fileButtons[0].length won't work here as expected, so I comment it out
let fileButton = document.getElementById('filemanagement_uploadbutton[0]');
with the appropriate changes to the code below when I reference them.
Share Improve this question edited Nov 18, 2024 at 19:06 Steve G. asked Nov 18, 2024 at 17:46 Steve G.Steve G. 4092 gold badges10 silver badges24 bronze badges 8-
The first error I'm getting is
Uncaught ReferenceError: uploadFiles is not defined
fromonclick="javascript:uploadFiles();"
. Post a minimal reproducible example please – j08691 Commented Nov 18, 2024 at 17:54 -
FYI there's no need for
javascript:
inonXXX
attributes. That's only needed when putting JavaScript inhref
– Barmar Commented Nov 18, 2024 at 18:05 - 1 Can't reproduce: jsfiddle/barmar/wk6qrho9 – Barmar Commented Nov 18, 2024 at 18:07
-
I think the specific problem of your code is that
.getElementsByName()
return aNodeList
(in most user agents, except IE and Edge), which is an array of nodes (not elements). And nodes don't have.click()
functions... Then, you should accessfileButtons[0].nodeValue.click()
. – KLASANGUI Commented Nov 18, 2024 at 18:26 -
@KLASANGUI That's not how
nodeValue
works. See developer.mozilla./en-US/docs/Web/API/Node/nodeValue – Barmar Commented Nov 18, 2024 at 18:36
2 Answers
Reset to default 0Instead of using the inline HTML "onclick" function, you could try removing it and using an event listener in your JavaScript instead.
let fileButtons = document.getElementByName('filemanagement_uploadbutton');
if (fileButtons.length > 0) {
fileButtons[0].addEventListener('click', uploadFiles);
fileButtons[0].click();
}
You are need to have your code something like this.
First you need get the element by id using document.getElementById('filemanagement_uploadbutton[0]')
and then add the add event listener.
addEventListener('click', () => {
// your code goes here
});
document.getElementById('filemanagement_uploadbutton[0]').addEventListener('click', () => {
alert('hi');
});
<button id="filemanagement_uploadbutton[0]" name="filemanagement_uploadbutton">Upload</button>
本文标签: JavaScript error on button elementclick is not a functionStack Overflow
版权声明:本文标题:JavaScript error: on button element, click is not a function? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745603824a2158612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Uncaught ReferenceError: uploadFiles is not defined
fromonclick="javascript:uploadFiles();"
. Post a minimal reproducible example please – j08691 Commented Nov 18, 2024 at 17:54javascript:
inonXXX
attributes. That's only needed when putting JavaScript inhref
– Barmar Commented Nov 18, 2024 at 18:05.getElementsByName()
return aNodeList
(in most user agents, except IE and Edge), which is an array of nodes (not elements). And nodes don't have.click()
functions... Then, you should accessfileButtons[0].nodeValue.click()
. – KLASANGUI Commented Nov 18, 2024 at 18:26nodeValue
works. See developer.mozilla./en-US/docs/Web/API/Node/nodeValue – Barmar Commented Nov 18, 2024 at 18:36