0

I have this jquery code:

$('.viewmap').click(function () {

    updateMap();

});

So when someone clicks a link with the viewmap class it runs a function called updateMap. In the updateMap function I create a variable like so:

var mapid = $(this).attr('title');

The variable above does not work, it has a undefined value, my question is in the viewmap click function how do I access the current data via $(this) in the updateMap function?

John
  • 9,840
  • 26
  • 91
  • 137

3 Answers3

0

one alternative is to add a parameter to updateMap and call updateMap($(this)); instead of updateMap();

earth_tom
  • 831
  • 1
  • 5
  • 13
0

pass a reference of this to the updateMap function

$('.viewmap').click(function () {
    updateMap(this);
});

function updateMap(obj) {
    var mapid = $(obj).attr('title');
    ...
}
Kao Vang
  • 412
  • 2
  • 3
0

There is two recomended ways:

    //If u don't want to do any work before the updateMap call
    $('.viewmap').click(updateMap);

    //If u want do do anything before updateMap call
    $('.viewmap').click(function(e) {
         updateMap.apply(this, arguments);
    });

Both ways will pass the this and every new argument jquery add now and in the future

Fagner Brack
  • 2,365
  • 4
  • 33
  • 69