admin管理员组文章数量:1292160
I use the querySelector
in JS to select html markup that I filled in with a JS script. However, anytime I try to store the divs with a class of .card
in the const questionCard
, I get null
.
Why can't I select the cards?
HTML:
<div class='card-container'></div>
JS:
const questionBlock = document.querySelector('.card-container');
const questionCard = document.querySelector('.card');
function build() {
let output = [];
...some unimportant code here...
output.push(
`
<div class='card'>
<div class='question'>${current.question}</div>
<div class='answers'>${answers.join('')}</div>
</div>
`
);
})
questionBlock.innerHTML = output;
}
build();
I use the querySelector
in JS to select html markup that I filled in with a JS script. However, anytime I try to store the divs with a class of .card
in the const questionCard
, I get null
.
Why can't I select the cards?
HTML:
<div class='card-container'></div>
JS:
const questionBlock = document.querySelector('.card-container');
const questionCard = document.querySelector('.card');
function build() {
let output = [];
...some unimportant code here...
output.push(
`
<div class='card'>
<div class='question'>${current.question}</div>
<div class='answers'>${answers.join('')}</div>
</div>
`
);
})
questionBlock.innerHTML = output;
}
build();
Share
Improve this question
edited May 2, 2023 at 12:14
3limin4t0r
21.2k3 gold badges37 silver badges58 bronze badges
asked Oct 12, 2017 at 0:03
bird_blue03bird_blue03
1471 gold badge2 silver badges9 bronze badges
5
-
your html does not have an element with class
card
before you runbuild()
– Jaromanda X Commented Oct 12, 2017 at 0:03 - It is created in the JS. – bird_blue03 Commented Oct 12, 2017 at 0:04
-
see edited ment -
questionCard
is not "dynamic" - the value is set when the code is run – Jaromanda X Commented Oct 12, 2017 at 0:04 -
1
irrelevant -
questionCard
is null, nothing changes just because you subsequently added an element – Jaromanda X Commented Oct 12, 2017 at 0:06 - Does this answer your question? Why does jQuery or a DOM method such as getElementById not find the element? – Heretic Monkey Commented Apr 9, 2021 at 13:39
2 Answers
Reset to default 3You need to call document.querySelector('.card')
after calling build()
. It cannot find HTML elements that do not exist yet.
const questionBlock = document.querySelector('.card-container');
function build() {
let output = [];
...some unimportant code here...
output.push(
`
<div class='card'>
<div class='question'>${current.question}</div>
<div class='answers'>${answers.join('')}</div>
</div>
`
);
})
questionBlock.innerHTML = output;
}
build();
const questionCard = document.querySelector('.card');
An alternative to the more correct answers is:
const questionCard = document.getElementsByClassName('card');
now: questionCard
is a live HTMLCollection
, and questionCard[0]
will be the first element with class including card
本文标签: javascriptdocumentquerySelector returns nullStack Overflow
版权声明:本文标题:javascript - document.querySelector returns null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741545538a2384573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论