New blog location
New blog location
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Website</title> </head> <body> <h1 id="title">Hello</h1> <input type="checkbox"> <button class="btn">Click Me</button> <ul id="list"> <li class="item"><a href="https://www.google.com">Google</a></li> <li class="item">Second</li> <li class="item">Third</li> </ul> </body> <footer> <script src="index.js" charset="utf=8"></script> </footer> </html>
getElementsByTagName searches for elements with particular tag name. For example, document.getElementsByTagName("li) gives array of list items. document.getElementsByTagName("li")[2].style.color = "purple" to change the color.document.getElementsByClassName("btn")[0] gives the first item.document.getElementById just returns single item because ids are unique in a document.document.querySelector just returns a single item, but takes a CSS selector as input (element/class/id).document.querySelector("#title"). Use . for class.document.querySelector("li a"). Remember the space and read from right to left.document.querySelector("li.item")querySelectorAll. querySelector and its plural variety allows for complex selections. getElement methods are more broad. Generally, querySelector occurs more often.document.querySelector("h1").style.fontSize = "3rem".document.querySelector("button").classList.document.querySelector("button").classList.add("invisible"); This way one can create a separate style for the invisible class. This way all styles are inside stylesheet. One can also remove classes. There is also a toggle method.innerHTML, use the textContent. innerHTML gives the HTML in between the element tags. If there is a child tag, one gets back the HTML including child tag.document.querySelector("a").attributes gives a list of all attributes. document.querySelector("a").getAttribute("href"). To change it, use setAttribute which takes two parameters, setAttribute("href","https://www.bing.com");
Comments
Post a Comment