Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the element type of a matched element in jQuery?

I'm matching ASP.Net generated elements by ID name, but I have some elements which may render as text boxes or labels depending on the page context. I need to figure out whether the match is to a textbox or label in order to know whether to get the contents by val() or by html().

$("[id$=" + endOfIdToMatch + "]").each(function () {     //determine whether $(this) is a textbox or label     //do stuff }); 

I found a solution that doesn't work, it just returns "undefined":

$("[id$=" + endOfIdToMatch + "]").each(function () {     alert($(this).tagName); }); 

What am I missing?

like image 471
CMPalmer Avatar asked Dec 04 '08 20:12

CMPalmer


1 Answers

Just one jQuery too much:

$("[id$=" + endOfIdToMatch + "]").each(function () {     alert(this.tagName); }); 
like image 136
Tomalak Avatar answered Sep 26 '22 05:09

Tomalak