0

I want to compare two numbers on a webpage by using their Class names.

I have seen some answers using GetelementbyID("") but my webpage doesn't have any Id and it just have class. Can anyone help?

<span class="sc-hSdWYo ktoHrC">0.0023</span>'Number1
and
<span class="sv-hdESYo ioIng">0.0023</span>'Number2

I want this code to alert whenever Num1 become Bigger than number 2.

  • Try to use `getElementsByClassName('sc-hSdWYo')[0]` – Chaska May 06 '19 at 04:32
  • 1
    Possible duplicate of [How to access HTML element without ID?](https://stackoverflow.com/questions/236624/how-to-access-html-element-without-id) – baruchiro May 06 '19 at 04:41
  • thanks for your reply. when I use your code the alert is :object HTMLSpanElement. here is my code: ```var x=document.getElementsByClassName('sc-hSdWYo ktoHrC')[0]; alert(x)``` – Mohammad May 06 '19 at 04:41
  • try to use `getElementsByClassName('sc-hSdWYo')[0].innerHTML` to get the text of span tag – arisalsaila May 06 '19 at 04:46
  • Thanks you all, Specially @arisalsaila. It worked! – Mohammad May 06 '19 at 05:03

1 Answers1

0

you can try using document.getElementsByClass name method

num1 = document.getElementsByClassName('sc-hSdWYo ktoHrC')[0].innerHTML;
num2 = document.getElementsByClassName('sv-hdESYo ioIng')[0].innerHTML;

if( num1 > num2 ) {
//alert code goes here
}
yunzen
  • 32,854
  • 11
  • 73
  • 106