Selecting particular element using ID selector

In this post of JQuery, I will introduce you to id selector. The syntax of id selector is particular element’ attribute id value prepended by ‘#’ symbol, for eg #button.

It is used to select one particular element whose id matches the id given in the $ method.

Below is an example


1  <html>
2   <head>
3       https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
4   </head>
5   <body>
6       
7           function c() {
8               var $button = $('#button');
9               alert($button);
10          }
11      
12      <input id="button" name="button" type="button" value="button" onclick="selectElement();"/>
13  </body>
14 </html>

In the above html code, on click of button I am calling javascript selectElement method.

In the javascript method at line 8, I am selecting all the elements having id “button”, in the case of this example only one element has id ‘button’.

Leave a Reply