0

What I want to approach is to create a custom Function that looks like this:

access('h1').content('Hello World');

And this is how I tried to accomplish that:

function access(Tag)
{
  var Element = document.getElementsByTagName(Tag)[0];

  this.content = function(Content)
  {
     if (Content !== undefined)
          return Element.innerHTML = Content;
     else return Element.innerHTML;
  }

  return Element;
}

So this Function shall be in the verge of changing the Content or just returning it. Unfortunately it does not work this Way.

So far I played with it for about an Hour to rewrite it and find a Solution on my own, but I have not been successfull and I also do not know how to describe this Problem to Google it.

How can I solve this it?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Daniel Messner
  • 2,305
  • 2
  • 15
  • 15

1 Answers1

2

Replace this with Element in your code.

function access(Tag)
    {
      var Element = document.getElementsByTagName(Tag)[0];
    
      Element.content = function(Content)
      {
         if (Content !== undefined)
              return Element.innerHTML = Content;
         else return Element.innerHTML;
      }
    
      return Element;
    }
    
access('h1').content('Hello World');
<h1>Hello</h1>

Or try something like this.

function access(Tag)
    {
      var Element = document.getElementsByTagName(Tag)[0];
    
      var content = function(Content)
      {
         if (Content !== undefined)
              return Element.innerHTML = Content;
         else return Element.innerHTML;
      }
    
      return {content: content};
    }
    
access('h1').content('Hello World');
<h1>Hello</h1>
Sampath Liyanage
  • 4,776
  • 2
  • 28
  • 40