Get an attribute of a class in javascript

3

Hello, I'm developing a list with all my facebook friends.

I was able to create the first line of the code, which causes me to grab all my friends and store them in a variable inside an array, as shown below:

var info = document.getElementsByClassName("_52jh _5pxc");

Then when I ask to see the elements the following message is displayed:

info[0];
<h3 class="_52jh _5pxc"><a href="/urldouser?ref=bookmarks">Nome Do Usuário</a></h3>

What I need to do is to get the "User Name" and I'm not getting it, I tried to use

var names = info[0].getAtributte("a");

But it returns me null, how can I solve this, to the point of being able to get what it is after opening. >"Nome Do Usuário"< .

I think it's a simple answer, however, I'm starting the web phase now and I have some difficulties, thank you right away.

    
asked by anonymous 03.10.2016 / 20:10

1 answer

3

The a element is not an attribute of h3 , but a direct descendant, so you can not use getAtributte .

But you can use it like this, which uses the element as its starting point:

var names = info[0].querySelector("a").innerHTML;

You can actually get everyone doing it like this:

var nomes = [].map.call(document.querySelectorAll('._52jh._5pxc a'), function(el) {
    return el.innerHTML;
});

jsFiddle: link

    
03.10.2016 / 20:15