$(function(){ $("input").on("focus", function() { if ($(this).hasClass('textbox-text')){ var t = $(this).closest('.textbox').prev(); } else { var t = $(this); } console.log(t.attr("id")); });})