Identify and get element by ID, class or name - jQuery identifiers

Would you please provide an examples how to identify elements by their ID, class or name, by using jQuery function ?
05 Mar 2021 at 04:11 PM
0give a positive ratinggive a negative ratingshow more
StorStor
Hi,

To identify element by ID, class or name, check the examples below.


You can identify element by ID with # selector:

<script>
$(document).ready(function(){
$("#show_more").click(function(){
alert("Get element by ID using jQuery.");
});
});
</script>

<a href="javascript: void(0);" id="show_more" >Click to show more details</a>


To identify element by class, use . selector:

<script>
$(document).ready(function(){
$(".show_more").click(function(){
alert("Get element by ID using jQuery.");
});
});
</script>

<a href="javascript: void(0);" class="show_more" >Click to show more details</a>


To identify element by name, you may need a specific solution in some cases. The following example is focused on input text field, but the solution may be applied on many other elements:

<script>
$(document).ready(function(){
$("[name=user_id]").click(function(){
alert("You cannot change User ID.");
});
});
</script>

<input type="text" name="user_id" value="12345" readonly />

16 Mar 2021 at 02:37 PM
0give a positive ratinggive a negative ratingshow more
TimTim
Share on FacebookShare on TwitterShare on LinkedInSend email
Follow us on Facebook
2022 AnswerTabsTermsContact us