3

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.

enter image description here

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");
    }           
});
Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92

5 Answers5

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

CraftyFella
  • 7,520
  • 7
  • 47
  • 61
1

Take a look at DOM Events

https://en.wikipedia.org/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 ;-)

Tim Abell
  • 11,186
  • 8
  • 79
  • 110
1

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;
}

}
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

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.

Community
  • 1
  • 1
Marco Panichi
  • 1,068
  • 1
  • 18
  • 31
1

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