With Javascript, you can change HTML element content. To change content in HTML element, first you need to select it. There are several methods to select element using different aspects like id or class base.
In this article, we will go through different way to select HTML elements in Javascript. So lets start.
The getElementById() method returns the element with the id of specified value. This is one of the common methods used in Javascript to manipulate HTML element.
<div id="start">Start</div>
<script type="text/javascript">
var element = document.getElementById('start');
console.log(element);
// <div id="start">Start</div>
</script>
The getElementsByClassName() method returns object of all class name matched with query. Notice Elements plural word in method name.
<div class="point" id="start">Start</div>
<div class="point" id="end">End</div>
<script type="text/javascript">
var elements = document.getElementsByClassName('point');
console.log(elements);
// HTMLCollection [div#start.point, start: div#start.point]
</script>
getElementsByTagName() method returns object of all elements with the tag name.
<div class="point" id="start">Start</div>
<span class="point" id="middle">Middle</span>
<div class="point" id="end">End</div>
<script type="text/javascript">
var elements = document.getElementsByTagName('div');
console.log(elements);
// HTMLCollection [div#start.point, start: div#start.point]
</script>
getElementsByName('name') method returns object of all elements specified with the name.
<input type="text" name="username">
<input type="password" name="password">
<script type="text/javascript">
var elements = document.getElementsByName('username');
console.log(elements);
// NodeList [input]
</script>
querySelector() method returns first element with specified attribute. If you want to return all the elements with matched, use querySelectorAll() method.
<div class="point">Start</div>
<div class="point">End</div>
<script type="text/javascript">
var element = document.querySelector('.point');
console.log(element);
// <div class="point">Start</div>
var elements = document.querySelectorAll('.point');
console.log(elements);
// NodeList(2) [div.point, div.point]
</script>
Below are the list of the selectors pass in these methods.
Selector | Description |
---|---|
p | All p elements |
#start | Id with value of start |
.point | All class with value of point |
p.start | All p elements with start class |
p .start | All start class with descendant of p elements |
h1, h2 | All h1 elements and h2 elements |
div + h1 | First h1 elements with with immediate after div element |
div > h1 | All h1 elements with parent of div element |
div ~ h1 | Every h1 elements precede with div element |
a[title] | All a elements with title attribute |
a[title=header] | All a elements with title value of header |
li:nth-child(2) | Every li elements that is second child of its parent. |
li:nth-last-child(2) | Every li elements that is second child of its parent, starting from last child. |
li:nth-of-type(2) | Every li elements that is second li of its parent. |
li:nth-last-of-type(2) | Every li elements that is second li of its parent, starting from last child. |
:not(h1) | Every elements except h1 elements. not() will select reverse |
This way, you can pass above parameter in querySelector() and querySelectorAll() methods to select elements.
I hope it will help you.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to catch cURL Errors in PHP
While working with API request using PHP...Bar Chart Example using Google Chart in Laravel 7
Hello Artisan Hope you are doing well...How to Build a Chrome Extension
Extensions in Google chrome and Add-ons...Laravel 8 Create Botman Chatbot Tutorial Example
Many websites uses automatic bot re...How to Generate a Timestamp in JavaScript
Use the Date.now() Method You can sim...