Skip to content Skip to sidebar Skip to footer

Javascript: Fetch List Of Usernames To Console/file

how can I use javascript to fetch a list of the content of all the username class to the console or preferably a file?

Solution 1:

Just select them using querySelectorAll('.username'), then .maptextContent:

let users = [...document.querySelectorAll('.username')].map(i => i.textContent)

console.log(users)
.user-id, .username, .user-year { display: inline; }
<divid="#main"><divclass="user-id">5</div><divclass="username">@user1</div><divclass="user-year">2018</div><br><divclass="user-id">6</div><divclass="username">@user2</div><divclass="user-year">2015</div><br><divclass="user-id">5</div><divclass="username">@user3</div><divclass="user-year">2012</div></div>

Solution 2:

You can use javscript and following code

var users= document.getElementsByClassName('username');

var users= document.getElementsByClassName('username');

for(i=0;i<users.length;i++){
console.log(users[i].innerText)
}
.user-id, .username, .user-year { display: inline; }
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="#main"><divclass="user-id">5</div><divclass="username">@user1</div><divclass="user-year">2018</div><br><divclass="user-id">6</div><divclass="username">@user2</div><divclass="user-year">2015</div><br><divclass="user-id">5</div><divclass="username">@user3</div><divclass="user-year">2012</div></div>

Post a Comment for "Javascript: Fetch List Of Usernames To Console/file"