Is it possible, using jQuery, to find out the type of an element with jQuery? For example, is the element a div, span, select, or input?
For example, if I am trying to load values into a drop-down list with jQuery, but the same script can generate code into a set of radio buttons, could I create something like:
$('.trigger').live('click', function () { var elementType = $(this).prev().attr(WHAT IS IT); });
Given a drop-down list with a button next to it with the trigger class, my elementType
variable should return select
upon the button being pressed.
The typeof operator when applied to the jQuery object returns the string "function" . Basically that does mean that jQuery is a function. But the typing sort of stops there.
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
on('click', function (e) { var id = $(e. target). attr('id'); console. log(id); });
Getting the element type the jQuery way:
var elementType = $(this).prev().prop('nodeName');
doing the same without jQuery
var elementType = this.previousSibling.nodeName;
Checking for specific element type:
var is_element_input = $(this).prev().is("input"); //true or false
also you can use:
$("#elementId").get(0).tagName
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With