How do I get > p > a working in querySelector ?
<div id="container">
<p>
<a href="#">Link</a>
</p>
</div>
<script type="text/javascript">
console.log(document.getElementById("container").querySelector("> p > a"));
</script>
SyntaxError: An invalid or illegal string was specified
How do I get > p > a working in querySelector ?
<div id="container">
<p>
<a href="#">Link</a>
</p>
</div>
<script type="text/javascript">
console.log(document.getElementById("container").querySelector("> p > a"));
</script>
Share Improve this question asked Jul 30, 2012 at 10:05 anjaneshanjanesh 4,2519 gold badges52 silver badges72 bronze badges 2SyntaxError: An invalid or illegal string was specified
- 1 You want to translate jQuery code to pure JS? Is that it? – Fabrício Matté Commented Jul 30, 2012 at 10:07
- possible duplicate of When using querySelectorAll, is there a way to reference the immediate children of the context node, without using IDs? – Rob W Commented Jul 30, 2012 at 10:10
1 Answer
Reset to default 11It's the leading >
that's stopping you. If you're going to use querySelector
you might as well get the container by that means, too, rather than using getElementById
.
document.querySelector("#container > p > a")