User Tools

Site Tools


java-script:hello-world

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java-script:hello-world [2023/08/08 22:25] odeftajava-script:hello-world [2023/08/08 23:12] (current) odefta
Line 3: Line 3:
 The following script will display 'Hello World' both in a popup window and in the browser's console. To execute the script, it must be run within a web browser.  The following script will display 'Hello World' both in a popup window and in the browser's console. To execute the script, it must be run within a web browser. 
 <note>You can view the console output by accessing the browser's developer tools.</note> <note>You can view the console output by accessing the browser's developer tools.</note>
 +
 +===== HTML + JS code in the same file =====
  
 <code javascript hello-world-js.html> <code javascript hello-world-js.html>
Line 30: Line 32:
  
 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. \\  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+__Example 1__
-<code js>+<code javascript>
 <div id="container"> <div id="container">
   <p class="text">Hello, World!</p>   <p class="text">Hello, World!</p>
 </div> </div>
 +</code>
  
 +You could use **document.querySelector** to select the <p> element with the class "text":
 +<code javascript>
 +const textElement = document.querySelector('.text');
 +console.log(textElement.textContent); // Outputs "Hello, World!"
 </code> </code>
 +
 +Or you could select it by its parent's ID:
 +<code javascript>
 +const textElement = document.querySelector('#container .text');
 +</code>
 +
 +**#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__:
 +<code javascript>
 +<div id="container" class="text">Hello World!</div>
 +</code>
 +
 +To select the text 'Hello World':
 +<code javascript>
 +const textElement = document.querySelector('#container.text');
 +</code>
 +**#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.
 +
 +<note important>
 +If you want to select elements with the class text that are inside an element with the ID container, use **#container .text**. \\ If you want to select an element that has both the ID container and the class text, use **#container.text**.
 +</note>
 +
 </note> </note>
  
Line 69: Line 99:
 </note> </note>
  
 +===== HTML + JS code in separated files =====
 +
 +HTML code:
 +<code html hello-world.html>
 +<!DOCTYPE html>
 +<html lang="en">
 +<head>
 +    <meta charset="UTF-8">
 +    <title>Hello World!</title>
 +</head>
 +<body>
 +    <h1>Hello World! from an imported script file</h1>
 +    <script src="./hello-script.js"></script>
 +</body>
 +</html>
 +</code>
 +
 +Java Script code:
 +<code javascript hello-script.js>
 +alert('Hello world!');
 +const h1 = document.querySelector('h1');
 +console.log(h1.textContent);
 +</code>
 +
 +<note tip>See also [[java-script:script-tag-placement|where to place the script tag]].</note>
  
java-script/hello-world.1691522715.txt.gz · Last modified: 2023/08/08 22:25 by odefta