views:

62

answers:

1

In the below table starting from <td>2</td> to the last everything is dynamic values from PHP.

So each row contains the selectbox and the textbox which are dynamic. If any of the values are changed I should be able to get the row number such as "2" or "3" or "4" or so on which was in td how do i get that how do I create an array with these values separated by ','(do we need use join).

 $("#btn_refAddNew2").click(function(){
     var totrows= $('#refTbl').attr('rows').length;
     for(i=2; i<totrows; i++){
     $(".MyClass").bind("change",function(){

     });

 }

HTML:

  <table id="myTable">
    <tr> 
     <th> Column1</th>
     <th>  Column2</th>
    </tr>


  <table id="refTbl" cellpadding="0" cellspacing="0" border=1 cellpadding=1 cellspacing=1 class="formTable">
    <tr> 
     <th>  </th>
     <th> Reference Code </th>
     <th> Reference Value </th>
     <th> </th>
    </tr>
    <tr>
        <td valign="top">1 </td>
     <td valign="top">     
      <select name="select1"  id="selct1" class="test" tabindex="" >
      <option></option>
      </select>
     </td>
     <td > 
      <textarea valign="top" cols="24" name="referencevalue1" id="referencevalue1"></textarea>
     </td>
    </tr> 
    <tr> 
     <td colspan="4"> 
      <input name="" type="submit" class="" id="add1" value="add" >
     </td>
    </tr>
     <tr>
  <td>2</td>
  <td> <select name='se_refcode2' class='MyClass' id='referencecode2' tabindex='2>
  <option value="dynamic">DYNAMIC Value</option>
  <option value="dynamic">DYNAMIC Value</option>
  <option value="dynamic">DYNAMIC Value</option>
  </select></td> 
  <td> <input name='tx_references2' id='referencevalue2' type='text' value='9' class='MyClass'  tabindex='2' size='20' maxlength='20'  ></td>
  </tr>
  <tr class="rowtext" height="10" onmouseout="this.className ='rowtext';" onmouseover="this.className = 'highlightrowtext';">
  <td>3</td>
  <td> <select name='se_refcode2' class='MyClass' id='referencecode2' tabindex='2>
  <option value="dynamic">DYNAMIC Value</option>
  <option value="dynamic">DYNAMIC Value</option>
  <option value="dynamic">DYNAMIC Value</option>
  </select></td> 
  <td> <input name='tx_references3' id='referencevalue3' type='text' value='8' class='MyClass'  tabindex='3' size='20' maxlength='20'  ></td>
  <td>4</td>
  <td> <select name='se_refcode4' class='MyClass' id='referencecode4' tabindex='4>
  <td> <input name='btn_refDelete' type='button' value='Delete' class='bttn_med' id='btn_refDelete'  data-value ='4' ></td></tr>
  <td> <input name='tx_references3' id='referencevalue3' type='text' value='8' class='MyClass'  tabindex='3' size='20' maxlength='20'  ></td>

@patrickdw:I wasnot able to post the comments

+1  A: 

Rather than binding an event to every input, I would recommend you instead listen for the event on the table element, and take advantage of the event bubbling mechanism in JavaScript.

This can be achieved using delegate() rather than click(). This approach is a lot more efficient, especially when dealing with a potentially large number of elements like in this situation.

$(document).ready(function () {
    var changedRows = [];

    $('#yourTable').delegate('input,select', 'change', function () {
        var self = $(this);
        changedRows.push(self.closest('tr'));
    });
});

You can get the row index of the changed row by retrieving the rowIndex attribute of the tr element:

$('#yourTable').delegate('input,select', 'change', function () {
    var self = $(this);
    var tr = self.closest('tr');
    var index = tr.attr('rowIndex');

    changedRows.push(tr);
});
Matt
@Matt:I have a small problem that is the event is being fired only when we change the input text and select text .the event should be able to fire if any one changes
Someone
@Matt: i want to get the list of the changed rows in to an array(changed rows ) separated by ','and i should be able to pass to a hidden variable .in the below code iam not getting the values var changedRows = []; $('#refTbl').delegate('input,select', 'change', function () { alert("Hello"); var self = $(this); var tr = self.closest('tr'); var index = tr.attr('rowIndex'); alert("index is "+ index); changedRows.push(tr); }); alert("Values of ChangedRows"+changedRows);
Someone
@Someone: I'm not sure what you mean about when you want the event to fire. My example will handle the event for when any select or input field that is a child of the table the event is delegated to, changes. If you want to keep an array of the row indexes, push the index variable rather than the tr variable onto changedRows. changedRows.join() will then return a comma separated list of row indexes that have changed. You can either write this to your hidden input there and then, or write it when it is needed.. I.e. when the form is submitted, using $('yourHiddenField').val(changedRows.join());
Matt
@Matt:Iam having the button say "Update" so when i click on this button i have your code and which is being fired for the second time $("#updbutton").click (function(){ //code given by you });
Someone