$(function() { var a; $("td").click(function() { var b = $(this).attr("id"); a ? a != b && (a=a.split(/\s+/),b=b.split(/\s+/),path = finder.findPath(a[0],a[1],b[0],b[1], grid), drawPath(path), a = null) : a = b }) });