0

I'm new to Javascript and I'm trying to copy text from all span elements with the class "copyme", sort them in reverse order, and concatenate the strings together. Once I do this, I am trying to add a paragraph containing this information to the div tag with id="copyhere"

Here's what I have so far:

    var copy= document.getElementsByClassName("copyme").innerHTML;
    var arr = [];
    for (x = 0 ; x < copy ; x++){
       arr.push(x);
   }
    arr.concat().reverse();
    document.getElementById('copyhere').innerHTML = arr;

Nothing happens when I do this.

Any thoughts on how to proceed? Thank you.

3 Answers 3

1

Here you go:

 var copy = document.getElementsByClassName("copyme");
    var arr = [];
    for (x = 0 ; x < copy.length ; x++){
       arr.push(copy[x].innerHTML);
   }
   arr.reverse();
   document.getElementById('copyhere').innerHTML = arr.join("");

If you have any questions, feel free to ask :)

Sign up to request clarification or add additional context in comments.

4 Comments

Simply pasting code doesn't really help people understand what's going on, perhaps a little explanation as to what you've done to fix the code? For example, innerHTML needs to happen as you inspect each item as getElementsByClassName returns an array/list of items...
@twoleggedhorse I DID append a "if you have any questions, feel free to ask :)" though.
@twoleggedhorse I agree (that I should have explained something), but I was just about to close my laptop when i saw the question and I said "one more".
Yes, I tend to do that too
0

var copy= document.getElementsByClassName("copyme");
    var arr = [];
console.log(copy)
    for (var i = 0 ; i < copy.length ; i++){
       arr.push(copy.item(i).innerHTML);
   }
    arr.concat().reverse();
    document.getElementById('copyhere').innerHTML = arr;
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="copyme">1</div>
  <div class="copyme">2</div>
  <div class="copyme">3</div>
  <div id="copyhere"></div>
</body>
</html>

4 Comments

You are not concatenating the array.
Good catch! :) toString is called implicitly, when browser is trying to render it. That leads to joining all element of the array with default(',') separator.
Yes, toString will be called, but it is better to use a method like join because it enables you to specify the delimiter, which is better IMO. Just wanted to point that out, the "." ending instead of a ":)" may have made it sound offensive :)
Yeah, I totally agree with you about the advantage of having more control on the output(especially for beginners)
0

This might help

  var copy= document.getElementsByClassName("copyme");
  var arr = [];
  for (var x = 0 ; x < copy.length ; x++) {
     arr.push(copy[x].innerHTML);
    }
  document.getElementById('copyhere').innerHTML = arr.reverse().toString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.