Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

select element in js

//<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");
Comment

How to select an Element in JavaScript

// 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
Comment

select() in javascript

// 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>
Comment

PREVIOUS NEXT
Code Example
Javascript :: add 1 year to current date javascript 
Javascript :: npm react router dom 
Javascript :: import stripe in es6 
Javascript :: Navigation timeout of 30000 ms exceeded 
Javascript :: LazyLoad for images, Videos and Iframes JavaScript and JQuery 
Javascript :: change hover css javascript 
Javascript :: document getelementsbyclassname not getting all elements 
Javascript :: javascript removing items looping through array 
Javascript :: how to insert image by javascript 
Javascript :: console.log regex 
Javascript :: check if date is after or before with moment 
Javascript :: javascript element edit value 
Javascript :: viewdata array mvc razor javascript 
Javascript :: js test letter lowercase 
Javascript :: js input validate excel file type 
Javascript :: regex exec fails twice 
Javascript :: what is the weight of an domz erazer and sharpner combined 
Javascript :: check radio button is checked jquery 
Javascript :: Javascript case insensitive string comparison 
Javascript :: linebreak eslint 
Javascript :: how to set header in angular 8post 
Javascript :: javascript array remove element 
Javascript :: ignores _id mongoose schema 
Javascript :: how to push the get variables without page reloading in Jquery 
Javascript :: c++ switch 
Javascript :: setrequestheader authorization bearer 
Javascript :: js string search 
Javascript :: how to calculate average of array in javascript 
Javascript :: js class method called when page loads 
Javascript :: how to create hyperlinks discord.js 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =