0

in the code below, I need to get the ID of the element that has raised the event

$(document).ready(function () { $(".selectors").live('change', function () { $.post("GetCategoriesByParentId/", { ID: $(this).val() }, function (data) { var idd = $(this).attr('id'); //here }); }); }); 

but the idd is always 'Undefined'. Why ?

2
  • the HTML to which the change event is bound would be useful. Commented Nov 16, 2010 at 19:22
  • 1
    In that context/scope, wouldn't this be the post object? Commented Nov 16, 2010 at 19:22

2 Answers 2

3

In the context of the $.post callback, the value of this will be set to something different to that of the live call. You'll need to cache the value of this:

$(document).ready(function () { $(".selectors").live('change', function () { var idd = this.id; $.post("GetCategoriesByParentId/", { ID: $(this).val() }, function (data) { // idd is now the id of the changed element }); }); }); 
Sign up to request clarification or add additional context in comments.

Comments

1

The $(this) inside your .post function is not actually the current element in the set you're iterating through in the parent loop. Fix:

$(".selectors").live('change', function () { $thisElement = $(this); $.post("GetCategoriesByParentId/", { ID: $(this).val() }, function (data) { var idd = $thisElement.attr('id'); //here }); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.