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>
4   </head>
5   <body>
7           function c() {
8               var $button = $('#button');
9               alert($button);
10          }
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