Mike S Mike S - 5 months ago 17
Javascript Question

How to call multiple elements by class and .innerHTML?

I am having problems calling multiple elements with the same class name when using .innerhtml. For instance the url I am working with is:

https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=hen

and when I enter the following code in chrome console I get:

document.getElementsByClassName('a-size-small a-color-secondary')

<span class="a-size-small a-color-secondary">by </span>
<span class="a-size-small a-color-secondary">Adore Plush Company</span>
<span class="a-size-small a-color-secondary">Get it by <span class="a-color-success a-text-bold">Tuesday, Jun 21</span></span>
...
...


But when I enter: document.getElementsByClassName('a-size-small a-color-secondary').innerHTML

I get "undefined". The goal is to get just the text between the span tags.

Can someone give me some guidance? I have tried with different variations including var and even Selection method. It worked with Selection for the first class element but didn't work at all with SelectionAll. Thanks for any help.

Answer

document.getElementsByClassName('a') will give the Object with class name .

for example class name is a and there are many spans with a , then you need get the span value by using index of that object

 var x =document.getElementsByClassName('a');

 console.log("value of spans"+x[0].innerHTML);

COdepen-http://codepen.io/pen/