0

This may seem like a duplicate question, and to some extent, it is, but I have already been through many similar questions, and sadly, none have suited my need. I would really appreciate problem-specific advice.

My main problem in the JavaScript code here is that I cannot access the values in the variables RememberText20 and RememberFullText, in function TextLimiter, from function ReadMoreLessText. The "Message" is an argument for the ReadMoreLessText function, which essentially matches the element clicked to the correct value in the aforementioned variables, which are themselves arrays.

*I know there is nothing wrong with the arrays themselves, as they retain their values as they are supposed to, because a simple alert() proves this. Similarly, there is nothing wrong with the Message argument, as the function ReadMoreLessText works fine with other values.

My simple problem is that I cannot access the values in the aforementioned variables, from the ReadMoreLessText function, although they are global variables, as they should be.

I would really appreciate a problem-specific answer here. Thank you in advance.

// JavaScript Document

//Start Text250

window.onload = function TextLimiter() {
  for (y = 0; y < 6; y++) {
    FullText = document.getElementsByClassName("Introduction")[y].innerHTML;
    TextLength = FullText.length;
    RememberFullText = [];
    RememberFullText[y] = FullText;
    var Text250 = FullText.substr(0, 250) + "...";
    RememberText250 = [];
    RememberText250[y] = Text250;
    if (TextLength > 250) {
      document.getElementsByClassName("Read_More")[y].innerHTML = "Read More→";
      document.getElementsByClassName("Introduction")[y].innerHTML = Text250;
    } else {
      document.getElementsByClassName("Read_More")[y].innerHTML = "";
    }
  }
};

//End Text250


//Start ReadMoreLessText

var ReadMore = function(Message) {
  var ScreenText = document.getElementsByClassName("Introduction")[Message].innerHTML;
  if (ScreenText === RememberText250[Message]) {
    document.getElementsByClassName("Introduction")[Message].innerHTML = RememberText250[Message];
  } else {
    document.getElementsByClassName("Introduction")[Message].innerHTML = RememberText250[Message];
  }
};

//End ReadMoreLessText
Tushar
  • 85,780
  • 21
  • 159
  • 179
Chamika Fonseka
  • 29
  • 1
  • 1
  • 7

2 Answers2

0

I don't see your variables declared as globals. Do you have a var RememberText20, RememberFullText; outside any function?

Shanoor
  • 13,344
  • 2
  • 29
  • 40
  • @DanielFlint Javascript [automatically generates global properties](http://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function) (of `window` in browsers) if an asignment is made to an undefined identifier.While I don't recommend doing this, and it is not supported by ES6 under strict mode, I believe it explains why this code is working in this case. – traktor Oct 25 '15 at 20:43
0

Try defining RememberFullText and RememberText250 outside the enclosing for loop.

window.onload = function TextLimiter() {
    RememberFullText = [];
    RememberText250 = []
    for (y = 0; y < 6; y++) {
       ...

As written they are set to an empty array in each iteration of the loop. Hence only the last entry of each array will be retained after the loop has finished.

traktor
  • 17,588
  • 4
  • 32
  • 53
  • They still won't be visible to the `ReadMore` function, as they're scoped to `TextLimiter`. They'll need to be defined _outside_ of the `TextLimiter` function. – Daniel Flint Oct 25 '15 at 11:06
  • I've declared the variables within the first function, but outside of the loop, and although the array is empty, it works fine. The "Message" argument remains unchanged – Chamika Fonseka Oct 25 '15 at 11:45