Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JavaScript querySelector - By ID

<p id="id1">1st paragraph with id = id1</p>
<p id="id1">2nd paragraph with id = id1 (incorrect use)</p>
<button onclick="getElement()">Get element</button>

<script>
  function getElement() {
    // selecting element by its id
    let element = document.querySelector("#id1");

    element.style.background = "lightgreen";
  }
</script>
Comment

when to use getelementbyid and queryselector

The querySelector method lets you retrieve an element using a CSS selector query. The getElementById method retrieves an element by its DOM ID.
Comment

PREVIOUS NEXT
Code Example
Javascript :: get parameter from url using jquery 
Javascript :: javascript copy items into another array 
Javascript :: javascript design patterns pdf 
Javascript :: how to find the particular key and value in json in javascript 
Javascript :: how to disable link in react 
Javascript :: window scroll top 
Javascript :: javascript onclick append a new row to table 
Javascript :: how to make unclicable legend chartjs 
Javascript :: javascript Program for Sum of the digits of a given number 
Javascript :: js fetch queryselector 
Javascript :: JS iterate over an array 
Javascript :: react styled functional component 
Javascript :: javascript object iterate 
Javascript :: ajax data not reflecting after refresh particular div jquery 
Javascript :: angular autocomplete displaywith 
Javascript :: p5js click on button 
Javascript :: save jshint 
Javascript :: find highest number in array javascript 
Javascript :: javaScript (DOM) HTML Element by Id 
Javascript :: js date get hours 
Javascript :: javascript equivalent of CTRL+F5 
Javascript :: angular radio box already showing checked 
Javascript :: falsy values in javascript 
Javascript :: arrow functions in es6 
Javascript :: how to swap two elements in an array javascript 
Javascript :: delete element javascript 
Javascript :: json array in hidden field not coming 
Javascript :: node js url download 
Javascript :: jquery change h1 text 
Javascript :: string length in javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =