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 ?
0
give a positive ratinggive a negative rating
05 Mar 2021 at 04:11 PM
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 />

0
give a positive ratinggive a negative rating
16 Mar 2021 at 02:37 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us