Question

I'm trying to make javascript application where a user can enter the kind of cookie she/he wants to make, and then select the amount of cookies they want. Once the user makes this selection and presses the "bake" button, the cookies appear randomly in the page. I had an assignment once where I had to generate random divs on a page, and it kind of gave me the idea for this, which is just for fun and practice.

My problem is that I want the user to be able to get unique information about the cookies he/she creates by clicking on them, and I can't get this to work.

Was it helpful?

Solution

(1) Click Event is not defined properly :

 aCookie.onlclick = cookie.display(); 

 // typing error onlclick

 // cookie.display() actually it call display function, doesn't give  reference

change to :

  aCookie.onclick = cookie.display; 

(2) set id "kind" to input box

(3) Current code set properties (id,kind,x,y) to instance of Cookie, not to element. Change code so that, the element is a parameter & set properties to it.

//pass element to set properties
function Cookie(elem,id, kind, x, y) {

    elem.id = id;
    elem.kind = kind;
    elem.x = x;
    elem.y = y;

    this.display = function () {
        alert("Cookie number: " + this.id + "; is a: " + this.kind + "; cookie : " +
            " and it is situated on coordinates ;  " + this.y + " and " + this.x + " on the cookie pan");
    }
}

Fiddle : http://jsfiddle.net/aslancods/CzgLj/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top