Why am I getting error TS2339 on querySelectorAll for checkbox property 'checked' does not exist
up vote
2
down vote
favorite
I am working on a reactJS app which was created in 2016 and I am trying to patch functionalities to it. The files are in .tsx extension and app compiler looks for all the typescript errors before rendering. So right now I am trying to implement select all checkboxes on clicking the master checkbox input, but as it stands right now I am getting TS2339 error when I call 'checkboxElement.checked = true'.
Master checkbox
<input type="checkbox" onChange={this.checkAllBoxes} />
Slave checkboxes
<Col lg={1}>
<input type="checkbox" />
</Col>
select all boxes method
checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<Element>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
}
As I can see in my console.log that checkBox has checked getter and setter method but I get the TS2339 error on compilation. It's hard for me to believe that I am facing this issue with such a fundamental functionality.
The ERROR: (I get this error logged twice in my compilation window)
error TS2339: Property 'checked' does not exist on type 'Element'.
I have tried changing the query from querySelectorAll to getElementByID and getElementsByClassname with the respective changes. I will prefer querySelectorAll but no strict guidelines. I have looked at this github link where they say they have solved this issue but it doesn't work for me.
javascript reactjs typescript
add a comment |
up vote
2
down vote
favorite
I am working on a reactJS app which was created in 2016 and I am trying to patch functionalities to it. The files are in .tsx extension and app compiler looks for all the typescript errors before rendering. So right now I am trying to implement select all checkboxes on clicking the master checkbox input, but as it stands right now I am getting TS2339 error when I call 'checkboxElement.checked = true'.
Master checkbox
<input type="checkbox" onChange={this.checkAllBoxes} />
Slave checkboxes
<Col lg={1}>
<input type="checkbox" />
</Col>
select all boxes method
checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<Element>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
}
As I can see in my console.log that checkBox has checked getter and setter method but I get the TS2339 error on compilation. It's hard for me to believe that I am facing this issue with such a fundamental functionality.
The ERROR: (I get this error logged twice in my compilation window)
error TS2339: Property 'checked' does not exist on type 'Element'.
I have tried changing the query from querySelectorAll to getElementByID and getElementsByClassname with the respective changes. I will prefer querySelectorAll but no strict guidelines. I have looked at this github link where they say they have solved this issue but it doesn't work for me.
javascript reactjs typescript
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am working on a reactJS app which was created in 2016 and I am trying to patch functionalities to it. The files are in .tsx extension and app compiler looks for all the typescript errors before rendering. So right now I am trying to implement select all checkboxes on clicking the master checkbox input, but as it stands right now I am getting TS2339 error when I call 'checkboxElement.checked = true'.
Master checkbox
<input type="checkbox" onChange={this.checkAllBoxes} />
Slave checkboxes
<Col lg={1}>
<input type="checkbox" />
</Col>
select all boxes method
checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<Element>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
}
As I can see in my console.log that checkBox has checked getter and setter method but I get the TS2339 error on compilation. It's hard for me to believe that I am facing this issue with such a fundamental functionality.
The ERROR: (I get this error logged twice in my compilation window)
error TS2339: Property 'checked' does not exist on type 'Element'.
I have tried changing the query from querySelectorAll to getElementByID and getElementsByClassname with the respective changes. I will prefer querySelectorAll but no strict guidelines. I have looked at this github link where they say they have solved this issue but it doesn't work for me.
javascript reactjs typescript
I am working on a reactJS app which was created in 2016 and I am trying to patch functionalities to it. The files are in .tsx extension and app compiler looks for all the typescript errors before rendering. So right now I am trying to implement select all checkboxes on clicking the master checkbox input, but as it stands right now I am getting TS2339 error when I call 'checkboxElement.checked = true'.
Master checkbox
<input type="checkbox" onChange={this.checkAllBoxes} />
Slave checkboxes
<Col lg={1}>
<input type="checkbox" />
</Col>
select all boxes method
checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<Element>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
}
As I can see in my console.log that checkBox has checked getter and setter method but I get the TS2339 error on compilation. It's hard for me to believe that I am facing this issue with such a fundamental functionality.
The ERROR: (I get this error logged twice in my compilation window)
error TS2339: Property 'checked' does not exist on type 'Element'.
I have tried changing the query from querySelectorAll to getElementByID and getElementsByClassname with the respective changes. I will prefer querySelectorAll but no strict guidelines. I have looked at this github link where they say they have solved this issue but it doesn't work for me.
javascript reactjs typescript
javascript reactjs typescript
edited Nov 8 at 14:24
asked Nov 8 at 8:45
Xavitoj Cheema
3192517
3192517
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.
The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
1
Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
– Xavitoj Cheema
Nov 8 at 8:57
1
done! stackoverflow has a time restricition before I can check it as such.
– Xavitoj Cheema
Nov 8 at 9:05
add a comment |
up vote
0
down vote
You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.
Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)
add a comment |
up vote
0
down vote
Iterate as for loop without casting as NodeListOf<Element>.
checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
if(allCheckBoxes){
for(let i = 0; i< allCheckBoxes.length ; i++){
console.log(allCheckBoxes[i]);
if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
allCheckBoxes[i].checked = true;
}
}
}
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.
The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
1
Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
– Xavitoj Cheema
Nov 8 at 8:57
1
done! stackoverflow has a time restricition before I can check it as such.
– Xavitoj Cheema
Nov 8 at 9:05
add a comment |
up vote
2
down vote
accepted
Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.
The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
1
Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
– Xavitoj Cheema
Nov 8 at 8:57
1
done! stackoverflow has a time restricition before I can check it as such.
– Xavitoj Cheema
Nov 8 at 9:05
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.
The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
Typescript has no way of knowing the returned HTML Elements are inputs (which do have the checked property.
The simplest solution is to assert that querySelectorAll returns a list of HTMLInputElement
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']") as NodeListOf<HTMLInputElement>;
allCheckBoxes.forEach(checkBox => {
console.log(checkBox);
if(checkBox && checkBox.checked) checkBox.checked = true;
});
answered Nov 8 at 8:54
Titian Cernicova-Dragomir
54k33250
54k33250
1
Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
– Xavitoj Cheema
Nov 8 at 8:57
1
done! stackoverflow has a time restricition before I can check it as such.
– Xavitoj Cheema
Nov 8 at 9:05
add a comment |
1
Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
– Xavitoj Cheema
Nov 8 at 8:57
1
done! stackoverflow has a time restricition before I can check it as such.
– Xavitoj Cheema
Nov 8 at 9:05
1
1
Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
– Xavitoj Cheema
Nov 8 at 8:57
Thanks, this is the right answer. It was my responsibility to understands the expected return from a query like that.
– Xavitoj Cheema
Nov 8 at 8:57
1
1
done! stackoverflow has a time restricition before I can check it as such.
– Xavitoj Cheema
Nov 8 at 9:05
done! stackoverflow has a time restricition before I can check it as such.
– Xavitoj Cheema
Nov 8 at 9:05
add a comment |
up vote
0
down vote
You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.
Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)
add a comment |
up vote
0
down vote
You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.
Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)
add a comment |
up vote
0
down vote
up vote
0
down vote
You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.
Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)
You are casting the allCheckBoxes list to a NodeListOf, but Element has (correctly) no checked property.
Instead, you should cast it to NodeListOf, which is the proper interface for checkboxes (checked property is defined only for elements)
answered Nov 8 at 8:55
Keilath
1585
1585
add a comment |
add a comment |
up vote
0
down vote
Iterate as for loop without casting as NodeListOf<Element>.
checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
if(allCheckBoxes){
for(let i = 0; i< allCheckBoxes.length ; i++){
console.log(allCheckBoxes[i]);
if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
allCheckBoxes[i].checked = true;
}
}
}
}
add a comment |
up vote
0
down vote
Iterate as for loop without casting as NodeListOf<Element>.
checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
if(allCheckBoxes){
for(let i = 0; i< allCheckBoxes.length ; i++){
console.log(allCheckBoxes[i]);
if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
allCheckBoxes[i].checked = true;
}
}
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Iterate as for loop without casting as NodeListOf<Element>.
checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
if(allCheckBoxes){
for(let i = 0; i< allCheckBoxes.length ; i++){
console.log(allCheckBoxes[i]);
if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
allCheckBoxes[i].checked = true;
}
}
}
}
Iterate as for loop without casting as NodeListOf<Element>.
checkAllBoxes() {
const allCheckBoxes = document.querySelectorAll("input[type='checkbox']");
if(allCheckBoxes){
for(let i = 0; i< allCheckBoxes.length ; i++){
console.log(allCheckBoxes[i]);
if(allCheckBoxes[i] && allCheckBoxes[i].checked) {
allCheckBoxes[i].checked = true;
}
}
}
}
answered Nov 8 at 8:59
front_end_dev
1,3151511
1,3151511
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204174%2fwhy-am-i-getting-error-ts2339-on-queryselectorall-for-checkbox-property-checked%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown