views:

1247

answers:

5

I'm currently trying to hide a dynamically created table row after a button has been pressed. So far I have managed to handle part of the dynamic functions.

Each dynamic row has a "Cancel" and "Save" button, I have managed to respond to these with ease. My problem is actually working with the row itself.

$(function() {
    $(".add").click(function(){
  // Just append to the table
  $("table#bookmarks tr:first").after("<tr class='new' id='<?php echo rand(1, 9999); ?>'><td></td><td><b>URL:</b> <input type='text' id='newURL' /><br /><b>Title:</b> <input type='text' id='newTitle' /><br /><b>Description:</b><br /><textarea id='newDesc'></textarea></td><td><b>Tags:</b> <input type='text' id='newTags' /></td><td><a href='#' class='save'>Save</a><br /><a href='#' class='cancel'>Cancel</a></td></tr>");
  $('span#links').html('<i style="color: #FF0000">You must reload to recount links!</i>');
  // Actually, the user doesn't want to add another link
  $('.cancel').click(function() {
   $(this).parents(".new").animate({ backgroundColor: "#FF0000" }, "fast").animate({ opacity: "hide" }, "slow");
  });
  // Seems the user wants to add a link!
  $('.save').click(function() {
   $("table#bookmarks tr.new #id").animate({ backgroundColor: "#FFFFFF" }, "fast").animate({ opacity: "hide" }, "slow");
  });
 });

});

I need to now hide the row, I have tried all sorts of various methods, .parent, .attr to name a few.

Thanks for the help, it's much appreciated!

+1  A: 

You can use jQuery live, new since jQuery 1.3.2, to preserve the click function on a dynamically created item.

http://docs.jquery.com/Events/live

Wolfr
Thank you for your response, I shall try adding this now.
James Brooks
jQuery live is a beautiful thing but it doesn't support ALL events - in your case it should be fine as click is supported but for custom events http://docs.jquery.com/Plugins/livequery provides the same functionality.
Ravi
Ravi, can you suggest where to position the code and how it should be called? I am pretty sure I have the elements being called correctly?
James Brooks
A: 

Sorry to sound so dumb, how would I go about adding this function to my code? I've previously tried using the jQuery live but to no avail.

James Brooks
+3  A: 

Try chaining your jQuery functions like this:

$(function() {
    $(".add").click(function() {
     $("<tr class='new'><td></td><td><b>URL:</b> <input type='text' id='newURL' /><br /><b>Title:</b> <input type='text' id='newTitle' /><br /><b>Description:</b><br /><textarea id='newDesc'></textarea></td><td><b>Tags:</b> <input type='text' id='newTags' /></td></tr>")
      .append($("<td></td>")
       .append($("<a href='#'>Save</a><br/>")
        .click(function() { $(this).parents(".new").animate({ backgroundColor: "#FF0000" }, "fast").animate({ opacity: "hide" }, "slow"); }))
       .append($("<a href='#'>Cancel</a>")
        .click(function() { $(this).parents(".new").animate({ backgroundColor: "#FF0000" }, "fast").animate({ opacity: "hide" }, "slow"); })))
      .insertAfter($("table#bookmarks tr:first"));
      $('span#links').html('<i style="color: #FF0000">You must reload to recount links!</i>');
    });
});

(This is a modified version of your original code so it's still a bit messy.)

Emmett
That worked brilliantly! Thank you Emmett!
James Brooks
A: 

I tossed together a quick example but I'll admit I'm a tad rusty on jQuery. This code however does work, at least for me.

$(function() {
$(".add").click(function(){
 var save = $("<a href='#' class='save'>Save</a>").click(function() {
  $(this).parent().animate({ backgroundColor: "#FFFFFF" }, "fast").animate({ opacity: "hide" }, "slow")
 })

 var cancel = $("<a href='#' class='cancel'>Cancel</a>").click(function() {
  $(this).parent().animate({ backgroundColor: "#FFFFFF" }, "fast").animate({ opacity: "hide" }, "slow")
 })

 var buttonTD = $("<td></td>");
 buttonTD.append(save);
 buttonTD.append(cancel);

 var row = $("<tr class='new' id='<?php echo rand(1, 9999); ?>'><td></td><td><b>URL:</b> <input type='text' id='newURL' /><br /><b>Title:</b> <input type='text' id='newTitle' /><br /><b>Description:</b><br /><textarea id='newDesc'></textarea></td><td><b>Tags:</b> <input type='text' id='newTags' /></td></tr>")
 .append(buttonTD);
});

});

Doomspork
Too many dom appends. Not a good maintainable solution.
redsquare
You dug this post up how many months after it was posted? How many appends? I think you might be a little rusty on your knowledge.
Doomspork
buttonTD.append(save);buttonTD.append(cancel); Should be done in one go. Appends cause a redraw which hurts performance.
redsquare
A: 

The jQuery live function:

$("#sendmail").live("click", function(){

    // your code goes here  


});

here is an example of how I used it.

$("#sendmail").live("click", function(){

 $("#emailres").html('<img src="../images/ajax-loader.gif">');
 var youremail = $("#youremail").val();
 var subject = $("#subject").val();
 var message = $("#message").val();

 $.ajax({
    type: 'post',
    url: 'email.php',
    data: 'youremail=' + youremail + '&subject=' + subject + '&message=' + message,

    success: function(results) {
  $('#emailres').html(results);
        }
    }); // end ajax 

});

To hide the selected row, do something like this:

first give your table an id (something like #mytable)

$("#cancel_row").live("click", function(){

 $(this).$("#mytable tr").hide();

});

hope that helps you.

A Hassan
Brilliant, that shows me a good example for using jQuery live. Thanks A Hassan!
James Brooks