INF105 gruppetime 06

☛ Handwritten notes can be included in the quizzes

Contents

  1. Browser’s Inspector
  2. JavaScript Syntax
  3. Inline styles and scripts
  4. Adding interactivity
  5. JavaScript Practice

Browser’s Inspector

We already opened browser’s developer tools on the previous step. Let’s have a closer look: right click on the web page → ‘Inspect’. You will see several panels there, first 4 of them are:

  • Elements, or Inspector - this the HTML code of the page. Here you can focus, edit or delete certain HTML tags, as well as inspect CSS Layout.
  • Console - this is what we will focus today. Here you can run JavaScript lines.
  • Source / Debugger tab - FIles associated with the page to debug code.

Task. Let’s try to play with Console tab:

  • Type console.log(‘Hello’). . What happened?
  • You can also list all the elements of the particular HTML tag:
    console.log(document.querySelectorAll('p'))
    
  • Or even change their background colours!
    document.querySelectorAll('p').forEach(el => el.style.background = 'pink')
    

JavaScript Syntax

Let’s stop for a moment on a latter example:

  document.querySelectorAll('p').forEach(el => el.style.background = 'pink')

You can read the latter line as:

  • querySelectorAll('p') - find all <p> elements in the HTML document
  • .forEach(el => ... - take them one by one (similarly to python’s for i in items)
  • ... => el.style.background = 'pink') - change each one’s inline style’s background color to pink.

Inline styles and scripts

It’s a good habit to maintain general styles.css and index.js files - but sometimes all you need is to change one element of the page. For this, you can write CSS properties or JavaScript functions right inside the html tag:

  • CSS - style attribute within HTML tag:
    <p style="color: red;"> Text </p>
    
  • JS - you can write the code with onclick attribute:
    <button type="button" onclick="document.getElementById('clock').innerHTML = Date()">
    <p id="clock"> </p>
    
  • Alternatively, you can write the script in the corresponding <script> tag.

Adding interactivity

JavaScript is a programming language that adds interactivity to websites - some elements that invite user actions and respond to it. For example, take a simple button:

  1. Create new HTML page (you may just copy your existing index or about page).
  2. Add the following line in page’s body:
<button id="recolor-btn" onclick=”change_color()”> Click me! </button>
<script src="index.js"> </script>
  1. In the same directory, create an index.js textfile with the following text:
function change_color() {
    document.body.style.background =
        document.body.style.background === "pink" ? "white" : "pink"; }
  1. Save the changes, update the page and click the button!

Each button click runs the JS function, changing the bg color of the page’s <body> element.

JavaScript Practice

Task. According to your idea about the website’s content and users, how interactivity may be useful for you? Try to implement it in you code. This might be something well-known, for example:

  • Change text colour according to background colour (dart theme like)


  • Hide all the images / tags of certain class on the website


  • Show a pop up notification on the website (like the one for cookies)


  • Or it could be something completely different - you call it!