0

I was making a function to change an element's text. It runs without throwing an error, but it doesn't do anything when I call the function.

var randomFunction = {
  text: function(element, text) {
    var slice = element.length;
    switch (element.charAt(0)) {
      case "#":
        var splice = element.slice(1, slice - 1);
        document.getElementById(splice).innerHTML = text;
        break;
      case ".":
        var splice = element.slice(1, slice - 1);
        document.getElementById(splice).innerHTML = text;
        break;
      default:
        document.getElementsByTagName(element).innerHTML = text;
        break;
    }
  }
};

// Usage: randomFunction.text("div", "Hello World");

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Those are some very, very confusing variable names. Consider using variable names more representative of what they contain. – CertainPerformance Apr 27 '18 at 01:55
  • 1
    Did you mean `getElementsByClassName` in the `"."` case? Why not just use [`querySelector`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) or [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll)? – Sebastian Simon Apr 27 '18 at 01:56
  • Note that you can `.slice(1, -1)` instead of explicitly referencing the length (but you probably just want `.slice(1)`). – Ry- Apr 27 '18 at 01:57

1 Answers1

-1

Getelementsbytagname results would be an array. Iterate over it and set the innerhtml property.

Abdul Rehman Sayed
  • 6,532
  • 7
  • 45
  • 74