9951 explained code solutions for 126 technologies


jqueryHow do I use jQuery to get an element by its ID?


Using jQuery to get an element by its ID is very simple. To do this, you can use the $(#id) selector. This will return a jQuery object containing all elements with the specified ID.

For example:

<div id="example">This is an example</div>

<script>
    var exampleElement = $('#example');
    console.log(exampleElement);
</script>

Output example

[div#example]

The code above does the following:

  1. Declares a <div> element with an ID of example.
  2. Uses the $('#example') selector to get the element with an ID of example.
  3. Stores the element in the exampleElement variable.
  4. Logs the exampleElement variable to the console.

Helpful links

Edit this code on GitHub