jQuery: Get selected element tag name
You can call .prop("tagName")
. Examples:
jQuery("<a>").prop("tagName"); //==> "A"
jQuery("<h1>").prop("tagName"); //==> "H1"
jQuery("<coolTagName999>").prop("tagName"); //==> "COOLTAGNAME999"
If writing out
.prop("tagName")
is tedious, you can create a custom function like so:jQuery.fn.tagName = function() {
return this.prop("tagName");
};
Examples:jQuery("<a>").tagName(); //==> "A"
jQuery("<h1>").tagName(); //==> "H1"
jQuery("<coolTagName999>").tagName(); //==> "COOLTAGNAME999"
Note that tag names are, by convention, returned CAPITALIZED. If you want the returned tag name to be all lowercase, you can edit the custom function like so:
jQuery.fn.tagNameLowerCase = function() {
return this.prop("tagName").toLowerCase();
};
Examples:jQuery("<a>").tagNameLowerCase(); //==> "a"
jQuery("<h1>").tagNameLowerCase(); //==> "h1"
jQuery("<coolTagName999>").tagNameLowerCase(); //==> "cooltagname999"
Need to get TAG name by element name using Jquery
use querySelector()
to retrieve the element and tagName
to retrieve the tag name
document.querySelector('[name="sample"]').tagName.toLowerCase();
Codepen demo
jQuery tag name of element
Try
this.nodeName
instead of $(this).nodeName
this
refers to the DOM element itself and $(this)
wraps the element up in a jQuery object.
EDIT
If you require Jquery approach you can use
$(this).prop("tagName")
Can jQuery provide the tag name?
$(this).attr("id", "rnd" + $(this).attr("tag") + "_" + i.toString());
should be$(this).attr("id", "rnd" + this.nodeName.toLowerCase() + "_" + i.toString());
jQuery: how to change tag name?
You can replace any HTML markup by using jQuery's .replaceWith()
method.
example: http://jsfiddle.net/JHmaV/
Ref.: .replaceWith
If you want to keep the existing markup, you could use code like this:
$('#target').replaceWith('<newTag>' + $('#target').html() +'</newTag>')
accessing TagName of jquery object
tagName
is a property of the underlying DOM element, not an attribute, so you can use prop
, which is the jQuery method for accessing/modifying properties:
alert($(obj).prop('tagName'));
Better, however, is to directly access the DOM property:alert(obj[0].tagName);
Get tagname name value javascript
Use document.querySelector
to get the element. It will return the first matched element.Then use getAttribute
to get the required attribute from the element.
If there are multiple tag element with same tagname , use document.querySlectorAll
var getElem = document.querySelector('preference'), getNameProperty = getElem.getAttribute('name');console.log(getNameProperty)
<preference name="webviewbounce" value="false" />
Does jQuery have any function to determine the tag type of the DOM element(s) referenced by jQuery object?
Do this:
this.nodeName;
...or to be safe, convert it to a specific case:this.nodeName.toLowerCase();
The nodeName
property is a property that is widely supported and will give you the tag name in the case of an element node (as in your case). How can I get name of element with jQuery?
You should use attr('name')
like this
$('#yourid').attr('name')
you should use an id selector, if you use a class selector you encounter problems because a collection is returned How to get tagname of the current selected element in jquery
Just get the value of a tagName
property:
var htmlElementName = this.tagName;
REF: https://developer.mozilla.org/en-US/docs/Web/API/Element.tagName
Related Topics
How to Keep an JavaScript Object/Array Ordered While Also Maintaining Key Lookups
Jsf/Primefaces Ajax Updates Breaks Jquery Event Listener Function Bindings
JavaScript Toisostring() Ignores Timezone Offset
How to Execute a Dynamically Loaded JavaScript Block
Play 2.X: How to Make an Ajax Request with a Common Button
Is There a Mechanism to Loop X Times in Es6 (Ecmascript 6) Without Mutable Variables
How Do Print the Console Output of the Page in Puppeter as It Would Appear in the Browser
Regular Expression to Match Exactly 5 Digits
Check If File Exists But Prevent 404 Error in Console from Showing Up
Why Do Empty JavaScript Arrays Evaluate to True in Conditional Structures
Regular Expression Not Working for at Least One European Character
How to Set Node_Env to Production/Development in Os X
Cross Domain Localstorage with JavaScript
Options for Testing Service Workers via Http
React 18, Useeffect Is Getting Called Two Times on Mount
Where Should I Initialize Pg-Promise
JavaScript - Telling Setinterval to Only Fire X Amount of Times
Dynamic Require in Requirejs, Getting "Module Name Has Not Been Loaded Yet for Context" Error