Can Jquery Provide the Tag Name

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



Leave a reply



Submit