// Find elements by tag name document.getElementsByTagName('H1'); // Find elements by class name document.getElementsByClassName('demo-class'); // Find a element that matches a specified CSS selector document.querySelector("#header") // Find all elements that matches a specified CSS selector document.querySelectorAll(".intro")
when a user clicks a button: document.getElementById("myBtn").addEventListener( "click", myFunction ); function myFunction() { console.log("Hello World!"); }
when a user clicks a button: document.getElementById("myBtn").addEventListener( "click", myFunction ); function myFunction() { console.log("Hello World!"); } Callback
an element document.getElementById("demo").innerHTML = "<h1>Hello World!</h1>"; // Change the attribute value of an HTML element document.getElementById("img1").setAttribute('src', 'http...');
a web server // and displays it function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); }
the selected elements $("p").append("Some appended text."); // Inserts content at the beginning of the selected elements $("p").prepend("Some prepended text.");