my code below is working on static elements but not those elements that is manipulated dynamically. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents () or the parentsUntil () method. I practicing jQuery, I came into a scenario which requires me to get the element name that is dynamically added inside the field div. The DOM tree: This method only traverse a single level up the DOM tree. The JavaScript getElementsByTagName() is a dom method to allows you to select an element by its tag name. The parent () method returns the direct parent element of the selected element. When you click the Rate button, the page will show an alert dialog that displays the your selected radio button value. Let rates = document.getElementsByName('rate') prop('tagName'), jQuery returns the tag name of the first element in the collection, so you get 'div' as a result. (this) contains the div, and when you run. Let btn = document.getElementById('btnRate') .add will add an element to the current jQuery collection, in this case, (this). The following syntax represents the getElementById() method: The JavaScript getElementById() is a dom method to allows you to select an element by its id. attr () or need a jQuery object containing that element for another reason, you can simply use (this).attr ('name') Inside an event handler, this.tagName should give you the name.
• JavaScript Get Element By Attribute Name JavaScript Get Element By Id Value Right, and one more (hopefully useful) hint: If row contains a list of