How can I see if an element is a "br"? I have tried to get it via .attr("type") and .type() but it always returns undefined! It would work on a input or button but not on a br element.
How can I see if an element is a "br"? I have tried to get it via .attr("type") and .type() but it always returns undefined! It would work on a input or button but not on a br element.
Share Improve this question asked Aug 24, 2012 at 6:17 mrmoormrmoor 3011 gold badge6 silver badges11 bronze badges 2 |4 Answers
Reset to default 9Use the .tagName
or .nodeName
property.
Then compare the value to "BR"
.
How about just $(element).is('br')
? For example:
<div class='tested' id='first'>
<br class='tested' id='second' />
</div>
...
$('.tested').each(function() {
var id = this.id;
if ($(this).is('br')) {
console.log(id + ' is <br>');
}
else {
console.log(id + ' is not <br>');
}
});
// first is not <br>
// second is <br>
by element.tagName
you can get the name of the tag
if (element.tagName=="br")
<div id="test">
<br>
</div>
--
var tag = $("#test").children().first().prop('tagName'); //returns BR
FIDDLE
var isTag = $("#test").children().first().is('br'); //returns true/false
FIDDLE
if (elem.localName == 'br') {...}
? – Ram Commented Aug 24, 2012 at 6:21.attr("type")
doesn't work on an input or button in the same sense: it doesn't tell you that the element is an<input>
or<button>
, it tells you the type of the<input>
or<button>
. – nnnnnn Commented Aug 24, 2012 at 6:23