0

I've got a variable called grabber that gets a class and I need to be able to compare the values of all the classes it grabs.

var grabber = document.getElementsByClassName("compareDiv");

It will return (5024)Votes or however many votes are listed on the div. I need to be able to compare them but from what I understand I would need to somehow make it an integer or omit certain characters so it can compare the value of the numbers? Any help is greatly appreciated!

  • Dupe: http://stackoverflow.com/questions/10003683/javascript-get-number-from-string – Marc B Oct 19 '16 at 22:02
  • Grabber doesnt "return" anything. Especially not a string. It contains a group of dom elements. Your question makes no sense .. – I wrestled a bear once. Oct 19 '16 at 22:07
  • My bad, so if it is returning the elements is there a way I could get just the number from it? Possibly convert it to a string then get just the number? – ShadoDart Oct 19 '16 at 22:17

1 Answers1

0

Iterate over elements, get their textContent and use parseInt() (or parseFloat() if values were fractional) to convert the value to a number:

var elements = document.getElementsByClassName('compareDiv'),
    count    = elements.length,
    numbers  = [];

for (var i = 0; i < count; i++) {
    numbers.push(parseInt(elements[i].textContent));
}

// The `numbers` array contains your numbers.
Marat Tanalin
  • 13,927
  • 1
  • 36
  • 52