Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need value from a named node map

I am going through an html structure to get a value I need. It appears to be a "NamedNodeMap" and I have gotten as far as this call:

ui.handle.attributes.getNamedItem("data-value")

which when printed in chromes console shows this:

data-value="12:00 AM"
    ->"12:00 AM"

I need to get the "12:00 AM" as a value, string, anything will work. I just dont know how to get it. .value on the end of my call does not work, i also tried .text and many other things.

Heres what the ui object looks like before i expand handle and attributes:

enter image description here

and then after:

html markup

html markup:

enter image description here

like image 410
connor moore Avatar asked Jun 15 '15 16:06

connor moore


People also ask

How do I access NamedNodeMap?

The nodes in the NamedNodeMap can be accessed through their name. The NamedNodeMap keeps itself up-to-date. If an element is deleted or added, in the node list or the XML document, the list is automatically updated. Note: In a named node map, the nodes are not returned in any particular order.

What is namenodemap?

The NamedNodeMap interface represents a collection of Attr objects. Objects inside a NamedNodeMap are not in any particular order, unlike NodeList , although they may be accessed by an index as in an array.

What is represented by NamedNodeMap?

A NamedNodeMap is an array-like unordered collection of an element's attributes. In other words: a NamedNodeMap is a list of Attr objects. A NamedNodeMap has a length property that returns the number of nodes. The nodes can be accessed by name or index numbes.

What is the purpose of the named node map object?

The NamedNodeMap object is used to represent collections of nodes that can be accessed by name.


3 Answers

.attributes appears to be a "NamedNodeMap" and I have gotten as far as calling .attributes.getNamedItem("data-value")

.attributes is a NamedNodeMap of Attribute nodes, which is a pretty deprecated interface. If you really want to use this, the .value, .nodeValue and .textContent properties of the attribute node should yield the text value you're after:

ui.handle.attributes.getNamedItem("data-value").value // "12:00 AM"

However, the standard approach would be to just use the getAttribute method of your element:

ui.handle.getAttribute("data-value") // "12:00 AM"

With HTML5, there is even the .dataset DOMStringMap specifically designed to access data attributes:

ui.handle.dataset["value"] // "12:00 AM"
like image 169
Bergi Avatar answered Oct 10 '22 19:10

Bergi


As you're using jQuery, there's a very simple way to get data-* attributes:

$('element').data('key');

So in your case, you'd need to find the ID of the element you are targeting and call:

$('#ID').data('value');

If ui.handle is a jquery object that represents a single node the you can do:

ui.handle.data('value');

Or if it's a javascript DOM node, then you'll need to wrap it in a $ call:

$(ui.handle).data('value');
like image 28
Toni Leigh Avatar answered Oct 10 '22 18:10

Toni Leigh


Element.attributes returns a NamedNodeMap of attributes of that HTMLElement which is mildly a JavaScript Map. Hence supposing

<span id="mySpan" name="test" message="test2"></span>

you can create an object from the NamedNodeMap like below:

const el = document.querySelector('#mySpan')
const attrs = Object.fromEntries(Array.from(el.attributes).map(item => [item.name, item.value]))

and then access an individual attribute by the dot notation for object properties:

console.log(attrs.name) // "test"
console.log(attrs.messsage) // "test2"
like image 1
shvahabi Avatar answered Oct 10 '22 19:10

shvahabi