jquery changes event for a table


I have a table with id #table1

I need that when the table changes (i use other code to do that) then runs some code, something like:


alert ("test")

Open in new window

any idea?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
Use the other code that changes the table to run additional code on completion.
joyacv2Author Commented:
my problem is the following,

my code add a row of three columns, then in the third cell i add a function that runs when the user click that cell. i try to use td.click, but if i include in that, the code runs more than once, so i decided to apply alone when the table changes
joyacv2Author Commented:
this is the code that .eliminar runs more than on time and not for each td individually

				var	str = $(this).html();
				var esDolar = str.search("\\$");
				if (esDolar == -1){
				var colIndex = parseInt($(this).index());
				var table = $(this).closest('table');
				var res = str.replace("$", "");
				$("#lista_articulos").append("<tr><td>"+$("tr:eq(0) td:eq("+colIndex+")",table).text()+" - "+$(this).parent().children(0).html()+"</td><td>"+$(this).html()+'</td><td><span class="eliminar">X</span></td><tr>')
					var	str = $(this).parent().prev().text();
					var res = str.replace("$", "");

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tom BeckCommented:
Okay, let me take a look.
Tom BeckCommented:
Why are the two click event handlers nested one inside the another? You need to separate them.
joyacv2Author Commented:
This is the part that don't understand why don't work

for example create a document.ready that apply a code over all td, but if I add another td later, then the new one don't runs the code, this is because document.ready already ran, but i need to apply the same code to every td that is create after, also, as you mention i don't know to make this work without separating
Tom BeckCommented:
The $("td").click() handles clicks of every td.

The $(".eliminator").click() handles clicks on the span inside the td.

When you click on the tds you are clicking both the td and the span inside so both handlers will fire once for each single click.

You don't need the second event handler. Just set up the td click event handler to handle both static< td>s as well as dynamically created <td>s like this:
$("body").on('click', 'td', function(){

Open in new window

joyacv2Author Commented:
ok, let me try
joyacv2Author Commented:
works, but i need only some td have this code attached that is why i try using span inside, how i modify the code that you provide to me?
Tom BeckCommented:
If you only want tds with <span class="eliminar"> to be affected then...
$("body").on('click', 'span.eliminar', function(){

Open in new window

If the only reason for the span in the first place is so you can attach an event handler then you don't need it. Put the class in the td, <td class="eliminar"> and...
$("body").on('click', 'td.eliminar', function(){

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joyacv2Author Commented:
Thank You very much Tom, you solve a problem that save me a lot of problems, thanks again! Excellent answer, the body... code that you provides is applied on the document ready and after i add another tr to the table, exactly what i looking for! Works perfect!!!!
Tom BeckCommented:
You're welcome. Thanks for the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.