Skip to content
the poor coder

get element by id in Jquery

Poor Man
Published
Reading time
2 min read
On this page

Get Element by ID in jQuery#

jQuery is a popular JavaScript library that simplifies HTML document traversing, event handling, and manipulation. It provides various methods to select, manipulate, and traverse HTML elements.

The jQuery ID Selector#

The jQuery $("#id") selector is used to select elements with a specified ID. It is similar to the native JavaScript document.getElementById() method.

Here's an example:

Text
<div id="myDiv">Hello World!</div>

<script>
  // Get the element with ID "myDiv"
  var myDiv = $("#myDiv");
  console.log(myDiv.text()); // Output: Hello World!
</script>

The JavaScript getElementById() Method#

The native JavaScript document.getElementById() method can also be used to get an element by its ID.

Here's an example:

Text
<div id="myDiv">Hello World!</div>

<script>
  // Get the element with ID "myDiv"
  var myDiv = document.getElementById("myDiv");
  console.log(myDiv.textContent); // Output: Hello World!
</script>

Using highlight.js for Syntax Highlighting#

highlight.js is a client-side syntax highlighting library that can be used to make code snippets look better. It supports a wide range of programming languages and markup formats.

To use highlight.js, you need to include its stylesheet and JavaScript files in your HTML document:

Text
<!-- Include the stylesheet -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/default.min.css">

<!-- Include the JavaScript file -->
<script src="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/highlight.min.js"></script>

Next, you need to call the hljs.highlightBlock() method to highlight the code inside a <code> tag:

Text
<div id="myDiv">Hello World!</div>

<pre><code class="javascript">
  // Get the element with ID "myDiv"
  var myDiv = $("#myDiv");
  console.log(myDiv.text()); // Output: Hello World!
</code></pre>

<script>
  // Call hljs.highlightBlock() to highlight the code
  hljs.highlightBlock(document.querySelector("pre code"));
</script>

Make sure to specify the language class in the <code> tag, as highlight.js uses it to determine which syntax to highlight.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC