how to access non-standard javascript properties

Asked

Viewed 43 times

1

Hello, I do an xml request, and I get a tag called item, with some unconventional attributes

$.ajax({
    type:"GET",
    dataType: 'text',
    url : "requests/playlist_jstree.xml",
    success: function(retorno){
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(retorno,"text/xml");
        var lista=xmlDoc.getElementById("plid_2").getElementsByTagName("item");
        console.log(xmlDoc.getElementById("plid_5").uri);
    }, 
    error: function(e){

    }
});

This plid_5 is inside plid_2, I could have accessed it as well:

console.log(lista[0].uri

But neither of the two can access this attribute called Uri, in fact the only attribute that works is id, the other attributes do not access, as I do to access the value of these attributes?

  • What gives console.log(retorno);?

  • Open the browser console and write js code directly there. You can even inspect the variable lista and see all its contents, methods and attributes.

  • JS is a language of Duck Typing, which means the language doesn’t care about strong signatures/content, everything will depend on how the object was built and fed

1 answer

0

If you refer to attributes represented in XML, you access them similarly as in the current class HTMLElement.

let elem = xmlDoc.getElementById `plid_5`

console.log(
    elem.attributes.uri.value,
    elem.attributes[índice].value,
    elem.getAttribute `uri`
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.