<h2 id="example/123">A heading</h2>
<script>
document.querySelectorAll("#example/123");
</script>
Any idea why above id is not working? do I need extra library to escape the /
?
<h2 id="example/123">A heading</h2>
<script>
document.querySelectorAll("#example/123");
</script>
Any idea why above id is not working? do I need extra library to escape the /
?
You can escape the / using \\
.
<h2 id="example/123">A heading</h2>
<script>
document.querySelectorAll("#example\\/123");
</script>
Duplicate of CSS selector to select an id with a slash in the id name?
However in this case you are using an id, so you can get the specific element without needing to get it from the NodeList using document.getElementById(), as only one element should have that id.
document.getElementById("hello/world");
Well, sometimes when newbies are learning JS so they make the mistake of linking the Javascript Link between two HTML Tags. Always use the Js linking scripts between the body tags.
Popular Solution :
Link your JS between HTML Tags :)
let para = document.querySelectorAll('h1');
para.forEach((allpara) => {
console.log(allpara)
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Learning Project</title>
</head>
<body>
<h1> This is 1st </h1>
<h1> This is 2nd </h1>
<h1> This is 3rd </h1>
<h1> This is 4th </h1>
<script src="index.js"></script>
</body>
</html>