Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Method 1: Per row closure</h1> <p><a href="http://jsfiddle.net/r6dx6/" rel="nofollow noreferrer">Here is a fiddle</a></p> <p>What you could do is use the <a href="http://api.jquery.com/each/" rel="nofollow noreferrer">each function</a> to create a closure for each row, in this case each row has its own changed variable. </p> <p>Also, rather than bind directly to the input element you can use the <a href="http://api.jquery.com/on/" rel="nofollow noreferrer">on() jQuery function</a> to bind the event listener indirectly.</p> <p><strong>Note:</strong> The inputs are not children directly off the row element so swap <a href="http://api.jquery.com/children/" rel="nofollow noreferrer">children()</a> for <a href="http://api.jquery.com/find/" rel="nofollow noreferrer">find()</a> as well.</p> <pre><code>$( document ).ready(function() { // Using the each function to create // a row specific closure $('.vraagrij').each(function() { // Track the state per row var changed = false // Create a variable to reference the // row directly in the change listener , $row = $(this); // Use event delegation to bind to all // descendant selects of the row efficiently $row.on("change", "select", function(e) { // Make sure the following only runs once // per row if(!changed) { changed = true; // Do something to the input, changing // color for testing purposes var $kids = $row.find("input"); $kids.css('color', 'yellow'); } }); }); }); </code></pre> <h1>Method 2: DOM query / traversal</h1> <p><a href="http://jsfiddle.net/r6dx6/4/" rel="nofollow noreferrer">Another fiddle</a></p> <p>This method uses the state of the DOM to track the changed state:</p> <pre><code>$(document).ready(function() { // Again using event delegation to bind events // efficiently $(".vraagrij").on("change", "select", function(e) { // Collect the parent row var $row = $(this).closest("tr"); // Check if it has already got the // changed class if(!$row.hasClass("changed")) { // Add the changed class in to track // the state and prevent this being // run again for the same row $row .addClass("changed") // Perform the action for the changed // select .find("input") .css("color", "yellow"); } }); }); </code></pre> <h1>Method 3: Use the one method</h1> <p><a href="http://jsfiddle.net/r6dx6/3/" rel="nofollow noreferrer">Another fiddle</a></p> <p>The <a href="http://api.jquery.com/one/" rel="nofollow noreferrer">one method</a> handles the event once.</p> <p><strong>Update</strong> including the great advice from <a href="https://stackoverflow.com/users/1087721/billyonecan">billyonecan</a> to use <code>closest("tr")</code> instead of <code>parents("tr:eq(0)")</code> or <code>parents("tr").first()</code></p> <pre><code>$(document).ready(function() { // Again using event delegation to bind events // efficiently, this time using the one method $(".vraagrij").one("change", "select", function(e) { // Perform the action for the changed // select $(this).closest("tr").find("input").css("color", "yellow"); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload