[jQuery] Re: inner loop values
Try: alert( $(this).attr('id') ); On 7/2/07, Phil Glatz [EMAIL PROTECTED] wrote: I'm building a dynamic table, assigning each row a unique ID based on the value of the counter, and trying to display the row id in an alert box when a row is clicked: $(function() { for (i=0; i5; i++) { var row_id = 'row' + i; var rw = 'tr id=' + row_id + 'tdrow ' + i + '/td/tr'; $('#t1').append (rw); $('#t1 tr#' + row_id).click( function() { alert(row_id); } ); } }); The problem is that although I get a different row_id for the id of each row, the alert box is always displaying row4. I understand why this is happening; this was the value at the last pass through the loop. What can I do to have it display the correct value? -- Benjamin Sterling http://www.KenzoMedia.com http://www.KenzoHosting.com
[jQuery] Re: inner loop values
$(function() { for (i=0; i5; i++) { var row_id = 'row' + i; var rw = $('tr id=' + row_id + 'tdrow ' + i + '/td/tr') .click( function() { alert(row_id); } ); $('#t1').append (rw); } }); - or - $(function() { for (i=0; i5; i++) { $('#t1').append('tr id=row'+ i +'tdrow '+ i +'/td/tr'); $('#t1 tr#row'+ i).click( function(){ alert(this.id); } ); } }); ~Sean
[jQuery] Re: inner loop values
Thanks for the suggestions, but I haven't found the answer yet. Try: alert( $(this).attr('id') ); This displays undefined in the alert box. var rw = $('tr id=' + row_id + 'tdrow ' + i + '/td/tr').click( function() { alert(row_id); } ); This displays row4 for all rows in the alert box. $('#t1 tr#row'+ i).click( function(){ alert( this.id); } ); This displays undefined in the alert box. original post: I'm building a dynamic table, assigning each row a unique ID based on the value of the counter, and trying to display the row id in an alert box when a row is clicked: $(function() { for (i=0; i5; i++) { var row_id = 'row' + i; var rw = 'tr id=' + row_id + 'tdrow ' + i + '/td/tr'; $('#t1').append (rw); $('#t1 tr#' + row_id).click( function() { alert(row_id); } ); } }); The problem is that although I get a different row_id for the id of each row, the alert box is always displaying row4. I understand why this is happening; this was the value at the last pass through the loop. What can I do to have it display the correct value? -- Benjamin Sterlinghttp://www.KenzoMedia.comhttp://www.KenzoHosting.com
[jQuery] Re: inner loop values
Phil, This is what I have and get the appropriate rowid in the alert: script $(function() { for (i=0; i5; i++) { var row_id = 'row' + i; var rw = 'tr id=' + row_id + 'tdrow ' + i + '/td/tr'; $('#t1').append (rw); $('#t1 tr#' + row_id).click( function(){ alert($(this).attr('id')); alert(this.id); }); } }); /script table tbody id=t1/tbody /table I am sure you already knew this, but you have to have the tbody in there, at least that is what I figured out from my past experience. On 7/2/07, Phil Glatz [EMAIL PROTECTED] wrote: Thanks for the suggestions, but I haven't found the answer yet. Try: alert( $(this).attr('id') ); This displays undefined in the alert box. var rw = $('tr id=' + row_id + 'tdrow ' + i + '/td/tr').click( function() { alert(row_id); } ); This displays row4 for all rows in the alert box. $('#t1 tr#row'+ i).click( function(){ alert( this.id); } ); This displays undefined in the alert box. original post: I'm building a dynamic table, assigning each row a unique ID based on the value of the counter, and trying to display the row id in an alert box when a row is clicked: $(function() { for (i=0; i5; i++) { var row_id = 'row' + i; var rw = 'tr id=' + row_id + 'tdrow ' + i + '/td/tr'; $('#t1').append (rw); $('#t1 tr#' + row_id).click( function() { alert(row_id); } ); } }); The problem is that although I get a different row_id for the id of each row, the alert box is always displaying row4. I understand why this is happening; this was the value at the last pass through the loop. What can I do to have it display the correct value? -- Benjamin Sterlinghttp://www.KenzoMedia.comhttp://www.KenzoHosting.com -- Benjamin Sterling http://www.KenzoMedia.com http://www.KenzoHosting.com
[jQuery] Re: inner loop values
Thanks Ben, works perfectly! On Jul 2, 6:12 pm, Benjamin Sterling [EMAIL PROTECTED] wrote: Phil, This is what I have and get the appropriate rowid in the alert: script $(function() { for (i=0; i5; i++) { var row_id = 'row' + i; var rw = 'tr id=' + row_id + 'tdrow ' + i + '/td/tr'; $('#t1').append (rw); $('#t1 tr#' + row_id).click( function(){ alert($(this).attr('id')); alert(this.id); }); }}); /script table tbody id=t1/tbody /table I am sure you already knew this, but you have to have the tbody in there, at least that is what I figured out from my past experience.