-1

Im trying to get an element but the class name is _2nlw _2nlv it has a space in it.

Here is a image to show you that the class name is what it is: https://i.stack.imgur.com/27fqh.png

The code im using is. var example = document.getElementsByClassName('_2nlw _2nlv').innerHTML;

Its not working and im wondering if it has something to do with the empty space in the class name? If so whats the solution?

  • 7
    A class in HTML cannot have a space in it, if it does, then those are 2 different classes – Rahul Desai Apr 19 '18 at 20:23
  • 1
    You should still look at [this question](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-getelementsbyclassname-and-other-getelementsby-method) and not call `.innerHTML` on the return. Also see comments on answers, you misspelled `getElementsByClassName` – ASDFGerte Apr 19 '18 at 20:26

3 Answers3

4

No such thing as a class with a space. That element has 2 classes and getElementsByClassName only searches for 1. You can use document.querySelector('._2nlw._2nlv') to match both.

Ben West
  • 4,398
  • 1
  • 16
  • 16
3

You can fetch elements having multiple classes, leaving a space between classes. In this purpose you just have to use getElementsByClassName:

var example = document.getElementsByClassName('class1 class2')[0].innerHTML;

Of course a class name can't have any space.

NayoR
  • 702
  • 6
  • 13
  • I find it ironic that you talk about using `getElementsByClassName` instead of `getElementByClassName` and then provide an example that uses the latter. –  Apr 19 '18 at 20:38
  • 1
    Interresting point, it's a bad copy / paste ahahah Removed from the original question, so I did so ! – NayoR Apr 19 '18 at 20:48
-4

you can use jquery with selector:

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<div class="_2nlw _2nlv">
text
</div>
<script>
$( "div._2nlw._2nlv" ).css( "border", "3px solid red" );
</script>

here :Link

Use the element first and next the class css

Documentation Selector Jquery

Diego Avila
  • 700
  • 7
  • 24