0

Hi I need some help with jquery; I've this HTML

<div id="1">1</div>
<div id="2">2</div>
<div id="3">3</div>
<label id="schedId"></label>

and I want to show in the label the div id where mouse is over. I tried with this:

for(var i = 1; i < 4; i++)
{
    $('#' + i.toString()).bind('mouseover', function () { 
        $("#schedId").text(i.toString());
    });
    $('#' + i.toString()).bind('mouseout', function () { 
        $("#schedId").text("");
    });
}

but I read always '4' in the label. Could you help me please? Thanks in advance.

Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
user2595496
  • 111
  • 1
  • 7

1 Answers1

0

Add a class of element to each div,

And you can do something like this:

$(".element").hover(
    function () {
        var id = $(this).attr("id");
        $("#schedId").text(id);
    }, function () {
        // mouseout code here 
    }
);

JSFiddle Demo

Nick R
  • 7,704
  • 2
  • 22
  • 32