0

I'm trying to create a similar function to the jquery .fadeOut() effect in pure javascript, but I'm having trouble with my code. The error code:

span[0].fadeOutEffect is not a function

My code:

var span = document.querySelectorAll("span");

function fadeOutEffect() {
  var node = this
  var fadeEffect = setInterval(function() {
    if (!node.style.opacity) {
      node.style.opacity = 1;
    }
    if (node.style.opacity > 0) {
      node.style.opacity -= 0.1;
    } else {
      clearInterval(fadeEffect);
    }
  }, 50);
}

span[0].fadeOutEffect();
<span>one </span><span>two </span><span>three </span>
Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
JMURP
  • 1
  • 2
    You created a function but it's not magically going to become a property of every `` element. – Pointy Jul 20 '18 at 14:31
  • 1
    To elaborate on Pointy's comment, if you want to fade `span[0]`, you would probably want to add the element as a *parameter*, like `function fadeOutEffect(element) { ... }` and call it via `fadeOutEffect(span[0]);`. – Tyler Roper Jul 20 '18 at 14:34

3 Answers3

1

When I read your code, I see you likely want to add a function to the HTMLElement prototype - it is not recommended but it will look like this:

HTMLElement.prototype.fadeOutEffect = function() {
  var node = this
  var fadeEffect = setInterval(function() {
    if (!node.style.opacity) {
      node.style.opacity = 1;
    }
    if (node.style.opacity > 0) {
      node.style.opacity -= 0.1;
    } else {
      clearInterval(fadeEffect);
    }
  }, 50);
}

var span = document.querySelectorAll("span");


span[0].fadeOutEffect();
<span>one </span><span>two </span><span>three </span>

A cleaner way is passing the span:

var fadeOutEffect = function(node) {
  var fadeEffect = setInterval(function() {
    if (!node.style.opacity) {
      node.style.opacity = 1;
    }
    if (node.style.opacity > 0) {
      node.style.opacity -= 0.1;
    } else {
      clearInterval(fadeEffect);
    }
  }, 50);
}

var span = document.querySelectorAll("span");


fadeOutEffect(span[0]);
<span>one </span><span>two </span><span>three </span>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

Your code is attempting to call a function, on a DOM element, which doesn't exist. Try passing the element as a parameter to your function instead.

var span = document.querySelectorAll("span");

function fadeOutEffect(node) {
  var fadeEffect = setInterval(function() {
    if (!node.style.opacity) {
      node.style.opacity = 1;
    }
    if (node.style.opacity > 0) {
      node.style.opacity -= 0.1;
    } else {
      clearInterval(fadeEffect);
    }
  }, 50);
}

fadeOutEffect(span[0]);
<span>one </span><span>two </span><span>three </span>
Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
Matt L.
  • 748
  • 5
  • 20
0

You are trying to add a function as a property to the array of span elements. You should instead pass it as a parameter to the function you created. Also, document.querySelectorAll("span"); returns an array of spans, as there can be multiple in your HTML document, so you can loop through each and apply you code to them.

See working example:

function fadeOutEffect(nodes) {
  nodes.forEach(function(node) { // Loop through each node (each span) in the array of spans
    var fadeEffect = setInterval(function() { // Run your code
      if (!node.style.opacity) {
        node.style.opacity = 1;
      }
      if (node.style.opacity > 0) {
        node.style.opacity -= 0.1;
      } else {
        clearInterval(fadeEffect);
      }
    }, 50);
  });
}

var nodes = document.querySelectorAll("span"); // Nodes is an array of spans
fadeOutEffect(nodes); // Pass the array of spans to your function
<span>Hello world!</span>
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64