jQuery

jQuery Check Element Type Examples

Did you want to check if element type is Div, Input, Blockquote etc.? In this article, I am going to share a very easy way to use jQuery to check the element type.

How to Check If Element Type is Div, Input, Select, Checkbox, Radio button using jQuery

Here is an example that checks if element type is Div or Unordered List or Input. When the “Check Element Type” button is clicked, an alert will be shown indicating the element type. This example can be very easily extended to check for any other element types. Just un-comment the element that you want to test in the code below (and comment the ones that you don’t) in order to alert the element type.

Example: Check Element Type using jQuery

<html>
<head>
<title>Use jQuery to Check if Element Type is Div, Input, etc.</title>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
</head>

<body>

<div id="check_this">Click the button!</div>

<input type="submit" name="check" id="check" value="Check Element Type" />

<script type="text/javascript">

$('#check').click(function() {

if( $("#check_this").is("div") )
{
alert('DIV');
}
else if( $("#check_this").is("ul") )
{
alert('UL');
}
else if( $("#check_this").is("input") )
{
alert('INPUT');
}
});

</script>
</body>
</html>
That’s it!

Do you know of any other ways to use jQuery to check element type? Feel free to suggest by commenting below.

One Comment on jQuery Check Element Type Examples

  1. 1

    Exactly what I was looking for! Thanks for sharing!

Leave a Reply to David Cancel reply

*

*