//<div id="demo-id"></div>
var elem1 = document.getElementById("demo-id");
//<div class="demo-class"></div>
var elem2 = document.getElementsByClassName("demo-class");
//<div name="demo-name"></div>
var elem3 = document.getElementsByName("demo-name");
//<p>Some text</p>
var elem4 = document.getElementsByTagName("p");
//<div id="demo-id"></div>
var elem1 = document.querySelector("#demo-id");
//<div class="demo-class"></div>
var elem2 = document.querySelectorAll(".demo-class");
//<div name="demo-name"></div>
var elem3 = document.querySelectorAll('[name="demo-name"]');
//<p>Some text</p>
var elem4 = document.querySelectorAll("p");
// Selecting an Element by id:
document.getElementById("elem-id");
// Selecting Elements by class:
document.getElementsByClassName("elem-class");
// Selecting Elements by tag name:
document.getElementsByTagName("div");
// Selecting Elements by name:
document.getElementsByName("elem-name");
/* This method is like a jQuery selector
( #elemId -> id, .elemClass -> class, h1 -> tag name) */
document.querySelector(".container"); // class
document.querySelector("#demo"); // id
document.querySelector("div"); // tag name
// select() in javascript
// add below code to your javascript file
function selectText() {
const input = document.getElementById('text-box');
input.focus();
input.select();
}
// add below code to your html file
<input type="text" id="text-box" size="20" value="Hello world!">
<button onclick="selectText()">Select text</button>