tags:

views:

59

answers:

1

I've got a function I am running on a jQuery object using .each(). Except, if a certain test passes, I actually want to run the function on the parent of the node in the current iteration. However, when I try to switch contexts, I get an assignment error or endless loop.

Here is the code that does not work:

$(this).each(function() {

if($(this).attr("title")) {
   content = $(this).attr("title");
   $(this) = $(this).parent();

  } else {
    content = $(this).html();
  } 

 //other stuff


}
+1  A: 

Sorry mate but you'll have to make a variable out of it.

$(this).each(function() {
    var mycontext;

if($(this).attr("title")) {
   content = $(this).attr("title");
   mycontext = $(this).parent();

  } else {
    mycontext = $(this);
    content = $(this).html();
  } 

 //other stuff using mycontext instead of $(this)


}
subtenante