It looks like you are using numbers as ids, here is a great answer on StackOverflow that describes how to create IDs: What are valid values for the id attribute in HTML?.
$(document).ready(function()
{
for(var i = 1; i < 8; i++)//see that I removed the $ preceeding the `for` keyword, it should not have been there
{
$("#"+i).hover(function() {
$(this).stop().animate({left:"50px"});//also notice that instead of using `"#" + i` as the selector inside the anonymous function I changed it to `this` so it properly references the hovered element
},
function() {
$(this).stop().animate({left:"30px"});
});
}
});
If you add a class to all of the elements you are binding to this can be majorly simplified:
$(function()
{
$(".hover-class").hover(function() {
$(this).stop().animate({left:"50px"});
},
function() {
$(this).stop().animate({left:"30px"});
});
});
Here is a demo of this solution: http://jsfiddle.net/FJQNa/
This will select all the elements with the hover-class
class and bind the mouseover
/mouseout
event handlers to them.
EDIT
You can also select multiple elements at once using ids by separating selectors with commas:
$(function()
{
$("#ele-1, #ele-2, #ele-3, #ele-4, #ele-5, #ele-6, #ele-7").hover(function() {
$(this).stop().animate({left:"50px"});
},
function() {
$(this).stop().animate({left:"30px"});
});
});
Docs for multiple selectors in jQuery: http://api.jquery.com/multiple-selector/
for
keyword doesn't have a$
.