Instead of using ids #r, let's include in the element the attribute data-something to differentiate the elements that must have the behavior setted by the function. Using a div, for instance: <div data-something="1_1"></div> <div data-something="2_2"></div> We won't need the ids with #r anymore. Considering that you already have the elements #h* we just need to put the id number inside data-something of the corresponding element, so that the function can use it: $("div[data-something]").hoverIntent(function() { var idNumber = $(this).data("something"); $('#h' + idNumber).toggleClass('vis'); }); Now, you just need to use the data-something attribute.