0

document.getElementByClassName('_2hvTZ pexuQ zyHYP')
The id within my class changes every time I reload the page. I need to find the id within the class f as shown below stays constant in the id as shown below with some ids. The class stays constant, so how do I find the new id within my class everytime. I would like to do this using Javascript RAW

f19c49e0e618e18 f8ca3372aa0bd8 f5c32cc227cf38

4
  • I removed the jquery tag sorry for troubling you young sir. Feb 21, 2019 at 19:54
  • the end is here, you cannot hack facebook. stop wasting your time with it Feb 21, 2019 at 19:56
  • can you try to answer my question? I am not trying to hack facebook xD Feb 21, 2019 at 19:56
  • 1
    How the OP seeks to use the information is not really our concern. Perhaps its about building a chrome extension or something, no clue. But the question is common one, and easily answered IMO.
    – Snowmonkey
    Feb 21, 2019 at 19:59

1 Answer 1

0
// We're going to get all the elements and force them into an array,
//  rather than a nodeList. That's what the [... and ] are doing to
//  our selector.
let myElements = [...document.getElementsByClassName('_2hvTZ pexuQ zyHYP')],
    arrayOfIds =  [];

// We can now use each member of myElements in a forEach. Within that,
//  the current member is called 'element'. So we can see its ID, and do
//  whatever we need to do with that.
myElements.forEach(function (element){ arrayOfIds.push(element.id);} );

console.log(arrayOfIds)

10
  • document.getElementByClassName(...) is not a function or its return value is not iterable Feb 21, 2019 at 20:01
  • make that document.getElementsByClassName(...) (notice the 's' on 'Elements') or document.querySelector('._2hvTZ pexuQ zyHYP');
    – Snowmonkey
    Feb 21, 2019 at 20:02
  • Execution of script 'New Userscript' failed! document.querySelector(...) is not a function or its return value is not iterable @snowmonkey Feb 21, 2019 at 20:04
  • I'm an idiot -- querySelector is not the iterable. document.querySelectorAll(...) returns an iterable list.Sorry about that.
    – Snowmonkey
    Feb 21, 2019 at 20:06
  • and it doesn't recognize the space in class names. So use just one part of that string.
    – Snowmonkey
    Feb 21, 2019 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.