Raf Raf - 20 days ago 5
Javascript Question

Jquery each display value of outside variable

Is it possible to display the value of a variable defined outside of $.each loop inside the loop. For eaxamle:

function fn(row, title) {
var row = $(row);
$.each(row, function() {
var that = $(this),
title = that.find(title);
});
}

fn('.row', '.title');


When I console log the title variable inside each function I get undefined. is it possible to access that variable inside the loop?

Thanks in advance for any help.

Answer

You're reassigning title with the result of the jQuery find:

title = that.find(title);

Maybe change it to:

var $title = that.find(title);

$title will now hold a jQuery object that you can do what you want with, and the original title won't change.