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"
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")
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
get tag name using jquery and javascript
You can use .tagName
, which is a property of HTMLElement
:
var tagNames = []; // Array of tag names
for(var i = 0; i < els.length; i++){
tagNames.push(els[i].tagName);
}
tagName
gives you the tag name in caps. You can convert it to small case by using .toLowerCase()
on .tagName
to get lower cased tag names.
Note :
As an alternative to tagName
, you could also use nodeName
. For tags, values of tagName
and nodeName
are identical!
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
get selectbox selected value by name tag jquery
Use .val()
on select
element not on selected option
:
$('select[name="country"]').on('change', function(){ alert($(this).val()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><select name="country"> <option value="IND" selected="selected">India</option> <option value="MY">Malaysia</option> <option value="US">United states</option> </select>
How to fetch name from select tag using jquery
If you want to get the attribute name of the select element, remove option:selected
from the selector. You can use attr()
to get specific attribute:
var getSelect = $('.myclass');console.log(getSelect.attr('name'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select class="myclass" id="" name="AP"><option value="">Select Val</option> <option value="50">50</option> <option value="60">60</option></select>
Getting element by tag name after getting a page using jQuery $.get
I tried using find but that did not work so I ended up using filter and that returned the value of textArea that I needed:
$.get( "/page.html").done(function( data ) {
var textArea = $(data).filter("textarea")[0].innerText;
});
jQuery get n child value outside of html tag
Replace the content in the span tag and use what remains.
$(function() {
const x = $("#test").text().replace($('#test > span').text(), "");
console.log(x);
console.log(Number(x) + Number(x));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="test">
<span>$</span>1.99
</span>
Get the real clicked element tag name for once with jquery
Use jQuery's stopPropagation()
method:
$("*").live('click',function(evt) {
evt.stopPropagation();
alert(evt.target.tagName);
})
JSFiddle demo.
Related Topics
How to Scroll to Top of Page With JavaScript/Jquery
Sorting Options Elements Alphabetically Using Jquery
Html Table to Excel JavaScript
Pure JavaScript Method to Wrap Content in a Div
How to Select Multiple Files With ≪Input Type="File"≫
Getcurrentposition() and Watchposition() Are Deprecated on Insecure Origins
Angularjs: No "Access-Control-Allow-Origin" Header Is Present on the Requested Resource
Getelementbyid() Returns Null Even Though the Element Exists
JavaScript Onclick Function Is Called Immediately (Not When Clicked)
Show Hide Divs on Click in HTML and CSS Without Jquery
How to Display JavaScript Variables in a HTML Page Without Document.Write
Open Window in JavaScript With HTML Inserted
Full Text Search in HTML Ignoring Tags/&
Referenceerror: $ Is Not Defined
Is There a Dom Event That Fires When an HTML Select Element Is Closed
How to Force a Link to Open in a Specific Browser
Strange Behavior When Iterating Over Htmlcollection from Getelementsbyclassname