Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have one interesting question.

I want to add rows to table using jquery, and after to make added row to respond to mouseover event, but code does not work.

The problem is that row is added as expected, but does not respond to mouseover event.

Any suggestions will be appreciated.

Urmas.

Code is following:

<head>
  <style type="text/css">
  .elem {
    color: purple;
    background-color: #d8da3d 
    }
  </style>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">
</script>   



</head>

<body>
<table id="table">
   <thead>  
    <tr>
        <th>Numbers</th>
        <th>Letters</th>
        <th>Symbols</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td class='elem'>111</td>
        <td class='elem'>aaa</td>
        <td class='elem'>@@@</td>
    </tr>
    <tr>
        <td class='elem'>222</td>
        <td class='elem'>bbb</td>
        <td class='elem'>###</td>
    </tr>
    <tr>
        <td class='elem'>888</td>
        <td class='elem'>iii</td>
        <td class='elem'>???</td>
    </tr>
    </tbody>        
</table>

<input type = "button" id = "add" value = "add row" />


<script type="text/javascript">

$(".elem").mouseover(function() {

alert("over");
});

$("#add").click(function() {

$row_to_add="";

$row_to_add+="<tr>";
    $row_to_add+="<td class='elem'>999</td>";
    $row_to_add+="<td class='elem'>qqq</td>";
    $row_to_add+="<td class='elem'>&&&</td>";
$row_to_add+="</tr>";

 $("#table tr:last").after($row_to_add);

});


</script>

</body>
share|improve this question

1 Answer 1

You have to use the on function to add event listeners to elements that you add dynamically. Eg:

$(document).on(".elem", "mouseover", function() {
    //...
}

Description: Attach an event handler function for one or more events to the selected elements.

(Per Mattias Buelens, who pointed out the live function is deprecated.)

share|improve this answer
    
Great! Thank You very much! This were the problem. –  Urmas Repinski Jun 3 '12 at 8:14
2  
Since jQuery 1.7, live is deprecated in favour of on, which is now your all-in-one binding function. $("#table").on("mouseover", ".elem", function() { ... }); –  Mattias Buelens Jun 3 '12 at 8:16
    
@MattiasBuelens but either one is acceptable, no? It looks like on is a more general function that can handle more scenarios. –  McGarnagle Jun 3 '12 at 8:22
    
live works well, this second is more complicated. –  Urmas Repinski Jun 3 '12 at 8:23
    
@dbaseman They both work, and most likely they'll keep working for a while. However, since live is deprecated, it may be removed from jQuery in some future release. It's very unlikely that they'll actually remove it, but for newly written code it's best to avoid using deprecated functions and instead use the latest and greatest functions. –  Mattias Buelens Jun 3 '12 at 8:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.