8

I need to get element by ClassName which starts with:

width-48 height-48 profile_view_img

The full tag:

<div class="width-48 height-48 profile_view_img_9131256"></div>

The numbers of this class always changes.

This is what I have tried so far:

var x = window.document.querySelectorAll("*[class^=\"width-48 height-48 profile_view_img\"]")[0]
Andrew
  • 1,507
  • 1
  • 22
  • 42

4 Answers4

5

Try the following:

var x = window.document.querySelectorAll("*[class^=\"width-48 height-48 profile_view_img\"]")[0]
var className = x.className;

JSFiddle.

Jacques Marais
  • 2,666
  • 14
  • 33
1

You could either use the querySelector like this:

var element = document.querySelector(".width-48.height-48.profile_view_img");

Or just use getELementsWithClass which accepts spaces like this:

var elements = document.getElementsByClassName("width-48 height-48 profile_view_img");
thepio
  • 6,193
  • 5
  • 35
  • 54
0

You're almost there.

Try:

var x = document.querySelectorAll('.width-48.height-48.profile_view_img')[0];

This reads as:

Let Variable X be the first element in the document which contains the classes .width-48 AND .height-48 AND .profile_view_img.

Rounin
  • 27,134
  • 9
  • 83
  • 108
-1

For the first element that has classes with names starting with

width-48 and height-48 and profile_view_img

you can try with:

var x = window.document.querySelectorAll("*[class^='width-48'] *[class^='height-48'] *[class^='profile_view_img']")[0]