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

PREVIOUS NEXT
Code Example
Javascript :: how to target an element inside of a $(this) jquery 
Javascript :: context api react 
Javascript :: sweet alert 2 react 
Javascript :: switch to window in testcafe 
Javascript :: react export 
Javascript :: dynamic button click event in jquery 
Javascript :: react form hook npm 
Javascript :: js random generator 
Javascript :: JavaScript try...catch in setTimeout 
Javascript :: js history 
Javascript :: get all data attributes jquery from multiple elements 
Javascript :: javascript check date is greater than today 
Javascript :: agregar atributo con id jquery 
Javascript :: wait for 1 second in loop in javascript 
Javascript :: how to remove item from array javascript 
Javascript :: datatable set row id 
Javascript :: get current location city name react native 
Javascript :: jquery templates 
Javascript :: vuetify open modal based on url anchor or # 
Javascript :: framer motion nextjs 
Javascript :: show a div in jquery 
Javascript :: es6 array to object keys 
Javascript :: bookmarklets 
Javascript :: date filter 
Javascript :: mongoose where 
Javascript :: discord.js create permanent invite 
Javascript :: how to select a class and then change the children of that class with javascript 
Javascript :: sort array of strings 
Javascript :: ios/main.jsbundle no such file or directory react native 
Javascript :: convert int to string in angular 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =