Hello World!
Hello World! from JS in a script tag
const element = document.querySelector(selector);
Here, selector is a string containing one or more CSS selectors separated by commas. This method will return the first element that is a descendant of the document that matches the specified selectors. \\
__Example 1__:
Hello, World!
You could use **document.querySelector** to select the element with the class "text":
const textElement = document.querySelector('.text');
console.log(textElement.textContent); // Outputs "Hello, World!"
Or you could select it by its parent's ID:
const textElement = document.querySelector('#container .text');
**#container .text** selector targets elements with the class text that are descendants of the element with the ID container. It doesn't matter how deeply nested the .text elements are within #container; they will be selected.
__Example 2__:
To select the text 'Hello World':
const textElement = document.querySelector('#container.text');
**#container.text** selector targets an element that has both the ID container and the class text. It's looking for a single element that matches both of these criteria.
If you're writing HTML that must conform to older standards or you want to be explicitly clear about the type, you can still include the type attribute. However, for most modern web development, it can safely be omitted.
Hello World!
Hello World! from an imported script file
Java Script code:
alert('Hello world!');
const h1 = document.querySelector('h1');
console.log(h1.textContent);