admin管理员组

文章数量:1326461

I have a Table and its rows are created dynamically.
I want to prompt a message [alert] when the tr or td is generated.

At first it will be like

<table class="tab"></table>

After that the rows will be added dynamically.

<table class="tab">
 <tr class="row">
  <td> Message1</td>
  <td> Message2</td>
 <tr>
</table>

The Class names for Table and TR will always same but message in TD will change.
I want to display the messages in ALERT when TD is added.

EDIT
It seams my question is not clear. I'm trying to implement this JSF thing from following link
CLICK HERE

Its a File Upload thing,when you upload wrong file then it will throw a error message in Table row, I want to alert a message when it happens.

ANSWER
Hello Every one Thanks for All of you answers. I got the Answer.

$(document).bind('DOMNodeInserted', function(e) {
    var element = e.target;
    if($(element).is("tr.tab")) {
    alert("New ROW ADDED");
    }           
});

I have a Table and its rows are created dynamically.
I want to prompt a message [alert] when the tr or td is generated.

At first it will be like

<table class="tab"></table>

After that the rows will be added dynamically.

<table class="tab">
 <tr class="row">
  <td> Message1</td>
  <td> Message2</td>
 <tr>
</table>

The Class names for Table and TR will always same but message in TD will change.
I want to display the messages in ALERT when TD is added.

EDIT
It seams my question is not clear. I'm trying to implement this JSF thing from following link
CLICK HERE

Its a File Upload thing,when you upload wrong file then it will throw a error message in Table row, I want to alert a message when it happens.

ANSWER
Hello Every one Thanks for All of you answers. I got the Answer.

$(document).bind('DOMNodeInserted', function(e) {
    var element = e.target;
    if($(element).is("tr.tab")) {
    alert("New ROW ADDED");
    }           
});
Share Improve this question edited May 27, 2013 at 12:18 Kishor Prakash asked May 24, 2013 at 10:41 Kishor PrakashKishor Prakash 8,17112 gold badges66 silver badges95 bronze badges 8
  • Post your javascript too – 웃웃웃웃웃 Commented May 24, 2013 at 10:43
  • 6 What is "dynamically" creating these rows? – ɴᴀᴛᴇ ᴅᴇᴠ Commented May 24, 2013 at 10:43
  • How are you adding the rows in your table using javascript – Manish Jangir Commented May 24, 2013 at 11:00
  • 1 Its another Framework called Primefaces, I can not control it. So i figured I'll handle it once Rows are created. – Kishor Prakash Commented May 24, 2013 at 11:00
  • 2 Tell whoever asked you to do this that this is a bad idea and will be fragile, hard to maintain and hard to troubleshoot. – Tim Abell Commented May 24, 2013 at 11:40
 |  Show 3 more ments

5 Answers 5

Reset to default 2

I think the solution to this can be simplified somewhat by using jqueries .on() event

Using this allows you to specify the event and selector in one statement:

$(document).on( "DOMNodeInserted", "tr.row", function() {
  alert( "New ROW ADDED" );
});

Here's a working JSFiddle

I suggest you a solution based on this discussion: How to detect new element creation in jQuery?

$(document).ready(function(){
    $(document).bind('DOMSubtreeModified',function(){
        $('.template-upload.ui-state-error').each(function( index ){
            alert( "The file '"+$(this).find('.name').html()+" is not valid" );
            $(this).find('.ui-icon-cancel').click();
        });
    })
});

Unfortunally I'm not a jquery expert, so check my code and do your experiments. Let me know if you need more help.

Take a look at DOM Events

https://en.wikipedia/wiki/DOM_events

"DOMNodeInserted - Fires when a node has been added as a child of another node"


Failing that, you could poll the DOM with a timeout, and add a class to everything you've already processed

setatimeout...
function timeouthandler (
  var newitems = $.(".tab td").not('.processed');
  if newitems {
     alert('new stuff!');
     newitems.addClass('processed')
  }
  setanothertimeout...
)

this is off the top of my head and needs some work. Feel free to edit this answer with something that actually works ;-)

You can use setinterval and write a function in such a way that if html for table is changed. then find the appended td and alert the message for user.

var previous_tr_html='';
setInterval(function() { $checkError(); }, 1000);

function checkError(){
  var current_tr_html=$('.row').html();
  if(previous_tr_html!=current_tr_html){
//new td added
  newerrortds=$('.row').find('td:not(.displayed)');
  $(newerrortds).each(function(){
  alert($(this).html();)
//add already displayed error class to current td
  $(this).addClass('displayed');
});
//change previous html value to current
 previous_tr_html=current_tr_html;
}

}

Extend the uploader script to report this by its own by event callback - the best way for me.

本文标签: javascriptHow to Detect Dynamically generated ROWS in TABLEStack Overflow