0

So I have some papers called "paper_0", "paper_1", etc. and I want to store them this way:

<h2 id="paper_0" onclick="doThing(this.id, 'myFunction')">Name of this thing</h2>

and in my javascript file:

//some code here
function doThing(id, func){
   currentId = id; //store current game
   func(); //this would create paper_0 **error**
   //some code here
}

function removePaper(){
   currentId.remove(); // **error**
}

so I have these 2 errors because id and func are both strings.

Any suggestion how can I store the paper objects? Or can I give IDs to papers?

Skeukry
  • 103
  • 2

1 Answers1

1

You are passing in a function name, not an actual function.

Try this instead:

function doThing(id, func){
   currentId = id; //store current game

   if(func == "myFunction") myFunction(currentId);

   //...
}

You can store the objects in a global variable:

var papers = array();

function myFunction(id) {
    papers.push(id);
}

To remove elements from an array, you can do this: How do I remove a particular element from an array in JavaScript?

function removePaper(id){
    papers.splice(array.indexOf(id), 1);
}

You may modify this dog object to suit your paper needs:

var myDog = {

    "name" : "Spot",

    "bark" : function() { alert("Woof!"); },

    "displayFullName" : function() {
        alert(this.name + " The Alpha Dog");
    }
};

myDog.displayFullName(); 

myDog.bark(); // Woof!
Community
  • 1
  • 1
Samuel Liew
  • 68,352
  • 105
  • 140
  • 225