How can I determine the type of an HTML element in JavaScript

0 votes
I need a way to determine the type of an HTML element in JavaScript. It has the ID, but the element itself could be a <div>, a <form> field, a <fieldset>, etc. How can I achieve this?
Oct 8, 2020 in Java-Script by kartik
• 37,520 points
2,480 views

1 answer to this question.

0 votes

Hello @kartik,

nodeName is the attribute you are looking for. For example:

var elt = document.getElementById('foo');
console.log(elt.nodeName);

Note that nodeName returns the element name capitalized and without the angle brackets, which means that if you want to check if an element is an <div> element you could do it as follows:

elt.nodeName == "DIV"

While this would not give you the expected results:

elt.nodeName == "<div>"

Hope it helps!!

Thank YOU!!

answered Oct 8, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How can I replace every instance of a string in JavaScript?

In JavaScript, replacing all instances of a ...READ MORE

answered 1 day ago in Java-Script by Navya
18 views
0 votes
1 answer

How do I delete a specific element from an array in JavaScript?

You can use the splice() method. Code: let arr ...READ MORE

answered 1 day ago in Java-Script by Navya
18 views
0 votes
1 answer
0 votes
1 answer

How can I iterate through the properties of a JavaScript object?

You can iterate through the properties of ...READ MORE

answered 1 day ago in Java-Script by Navya
36 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,178 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,044 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
3,001 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
45,074 views
0 votes
2 answers

How can I set focus on an element in an HTML form using JavaScript?

Hi Kartik, try the following script <script>  (window.onload = ...READ MORE

answered Sep 24, 2020 in Java-Script by Okugbe
• 280 points
1,898 views
0 votes
1 answer

How can I check the existence of an element in jQuery?

Hello @ Arpit In JavaScript, everything is 'truthy' or ...READ MORE

answered Sep 8, 2020 in Java-Script by Niroj
• 82,840 points
914 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP