jQuery select by ID
Here is the general syntax for using the jQuery select by id:
ID could be any value provided in the id attributes of the HTML element. You should use # followed by its ID in the selector.
For example, if there is a HTML element in the document like <div id=”text”> some text </div>, $(“#text”) will select the div with id equal to ‘text’ and do the intended action on it.
The main importance of this jquery selector is we will get separate control over each element by its unique id and the manipulations will affect only that element.
jQuery Select by ID Example
This example demonstrates the use of jQuery select by id and changing the CSS style of the selected element.
<title>jQuery select by ID</title>
$( "#div1" ).css( "border", "5px solid green" );
In this example you can see that <div> element with id=’div1′ is selected for manipulation.
Above HTML produces output as shown in below image.
jQuery select by id summary
Every HTML element can have unique id attribute. Therefore, it’s easy to get control over the element using the jQuery select by ID.
We can use this selector whenever we want to access a specific element to execute some task. Using this selector in combination could enhance the flexibility as well.