Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element type with jQuery

Tags:

jquery

element

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.

like image 285
Jamie Hartnoll Avatar asked Dec 05 '11 16:12

Jamie Hartnoll


People also ask

What is type in jQuery?

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.

What is $() in jQuery?

$() = 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.

How can get li id value in jQuery?

on('click', function (e) { var id = $(e. target). attr('id'); console. log(id); });


2 Answers

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 
like image 80
adeneo Avatar answered Sep 26 '22 02:09

adeneo


also you can use:

$("#elementId").get(0).tagName 
like image 43
Ali Avatar answered Sep 23 '22 02:09

Ali