Welcome to Discussion #2b!

CS 160 · User Interface Design and Development · Summer 2024 QR Code

Do Now: Get into your teams!

Back to index

Quick Refresher

Style elements with classes and ids


                    
                

Agenda

Learn DOM manipulation with Javascript


Our activity: Task Tracker

How do I include Javascript?


                    
                    

Where do I put script tag?

Read this!

Locating DOM Elements


                    

Theres More!


                                

We can manipulate HTML!


                    const para = document.getElementById('stylish-paragraph');
                    para.innerHTML = 'I say something else';
                    para.textContent = "I am a paragraph!"
                

Examples:

CSS & Event Handlers


                    

Examples:

Toggle Classes


                            

                    const btn = document.getElementById('btn');
                    const para = document.getElementById('p');
                    
                    btn.addEventListener('click', () => {
                        para.classList.toggle('bold');
                    });
                
Example

Functionality and Console


                    function add(a, b) {
                        return a + b;
                    }
                    
                    console.log(add(10, 5));

                    const subtract = (a, b) => {
                        return a - b;
                    };
                    
                    console.log(subtract(9, 2));
                

Lets look!

Input elements let us create interactive UI components for the user and we can get and set the value with JavaScript


                    
                

                const btn = document.querySelector('button');
                const inp = document.querySelector('#operand');

                btn.addEventListener('click', () => {
                  const num = parseInt(inp.value, 10); // .value
                  inp.value = num + num; // and it can be written!
                });
            
Example

Activity: Shopping List

https://codepen.io/Timothy-James-Aveni/pen/gOEvOOo

Thank you!


Other resources: