Asked 7 years ago
20 Jan 2017
Views 1522
jaydeep

jaydeep posted

$("#id") in jQuery to pure JavaScript ?

what are the other alternative of element selection at html Dom in JavaScript except $("#id") or $(".class")

i know that i can use id and class in jQuery to select by $("#id") or $(".classname") and make it operation on it like show or hide or change css etc.. but i want do it in pure JavaScript

why i am not using the jQuery . its only because i just need selector for hide a div for onload nothing more so its not good to use for just jQuery . it can be done also in pure JavaScript

Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

use document.getElementById is core JavaScript


document.getElementById("id-name-here")


now a days because of use of jQuery like JavaScript Library , people very rarely use core JavaScript .



Rasi

Rasi
answered Nov 30 '-1 00:00

use document.querySelector

it work with class

var qs = document.querySelector(".class-name");


it work with id

var qs = document.querySelector("#id-name");


it work with nested element

var qs = document.querySelector("span.info.main");

it work almost same as jQuery selector do

but document.querySelector Returns the first Element for given selection search. it good for id because for particular id its only one element but for classname and other selector it had more than one element
for that document.querySelectorAll is good

document.querySelectorAll return list of element .


Post Answer