jquery changes event for a table

Hi,

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:

$("#table1").change(function(){

alert ("test")
})

Open in new window


any idea?
LVL 1
joyacv2Asked:
Who is Participating?
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.
0
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
0
joyacv2Author Commented:
this is the code that .eliminar runs more than on time and not for each td individually

  $("td").click(function(){
		
				var	str = $(this).html();
				var esDolar = str.search("\\$");
				if (esDolar == -1){
					
				}else{
				var colIndex = parseInt($(this).index());
				var table = $(this).closest('table');
				var res = str.replace("$", "");
				$("#cart_sum").html(parseFloat(res)+parseFloat($("#cart_sum").html()))
				$("#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>')
				}
				
				$(".eliminar").click(function(){
					//$(this).closest('tr').remove();
					var	str = $(this).parent().prev().text();
					var res = str.replace("$", "");
					alert(res);
					$("#cart_sum").html(parseFloat($("#cart_sum").html()-parseFloat(res)))
					})
				
				
		})

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tom BeckCommented:
Okay, let me take a look.
0
Tom BeckCommented:
Why are the two click event handlers nested one inside the another? You need to separate them.
0
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
0
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

0
joyacv2Author Commented:
ok, let me try
0
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?
0
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

0

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!!!!
0
Tom BeckCommented:
You're welcome. Thanks for the points.
0
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
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.