i want to find and hide button "Add" in typescript.
How to do it ?
<div class="edittoolbar">
<div class="btn-group" role="group">
<button class="btn btn-primary" title="Add">
</button>
<button class="btn btn-primary" title="Modify">
</button>
<button class="btn btn-primary" title="Delete">
</button>
</div>
</div>
i using this code to find button but it not success.
because myNodeList1.length is return =0 , so i can not continue to find it.
please help me. Thank you!
const nodes = document.querySelectorAll('div.edittoolbar div.btn-group') ;
console.log(nodes);
const myNodeList1: NodeListOf<Element> = nodes.item(0).childNodes ; // node
console.log(myNodeList1.length) ;
i expecting have a ways to do it.
3
Answers
querySelector()
returns the first ElementchildNodes
returns all child nodes, including text nodes (including spaces and line breaks) and comment nodes. this can lead to unexpected results, so be careful.if you need purely element nodes, the
children
property is more appropriate.to access array elements by index you have to use []<‘square brackets’> , here you are using ()<‘parenthesis’>;
right way
change
I cannot reproduce your results.
Your code gives the following compile error:
I have tried this:
and the result is 7. Button ‘Add’ is
myNodeList1[1]
.A simple way to find the button with title="Add" is:
If you still have a problem, please share a complete, but small working application.