0

Alright, so I'm completely new to jQuery, so here goes:

I have a GameCard object which accepts a div in it's "constructor" which is then assigned to a variable. Within the object, I want to perform a function when that div is clicked.

function GameCard(imageSource, div)
{
    this.cardImage = new Image();
    this.cardImage.src = imageSource;

    this.hiddenImage = new Image();
    this.hiddenImage.src = HIDDEN_SOURCE;

    this.div = div;

    $(this).WHAT_HERE_?.click(function()
    {
    });
}

pretty much how do you refer to another variable within the same object using javascript?

Thanks in advance!

1
  • $(this.div).click(function() {}); or if you're doing the assignment inside of the constructor you can even use the div parameter. Commented Apr 3, 2013 at 22:30

2 Answers 2

3

You wrap the div using the jQuery function.

$(div).on('click',function(){
  //do stuff
});
Sign up to request clarification or add additional context in comments.

2 Comments

So if there's multiple objects of type GameCard, this function will only apply to the object it's wrapped within?
@JohnW it will only wrap the div. If you want multiple objects, you can place them in an array, and wrap that array with jQuery.
1

What is the id of your div?

Don't use $(this) in this context.

$("#IdOfDiv").click(function(){});

But wrap the above in this;

$(function(){});

This ensures that the jQuery code is not run until the document is loaded.

So;

$(function(){   $("#IdOfDiv").click(function(){
     //Do someting   
  }); 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.