views:

89

answers:

3

So the situation is for comments on a blog in progress, each commenter will have the ability to reply to any comment. To do this, I have a link that, when clicked, will reveal (with jQuery) a special reply form right there as opposed to the normal one at the bottom of the page. So instead of loading a form on every single comment when the page loads, I'd like to load only the forms that are needed by the user and only when they click on the link.

Now, I don't simply want to hide them, I know how to do that. I want them to not be there at all until that link is clicked. So can this be done? You don't need to tell me exactly how to do it (all though I may be back here later for something specific) If you just give me a search term, or a brief overview that would be awesome! Thanks!!

A: 

The easiest way to inject HTML is using jQuery's load function.

From the example - loads a page, and add a selection from the page:

$("#links").load("/Main_Page #jq-p-Getting-Started li");

For more advanced options, should you need them, you can also use ajax :

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});
Kobi
If the form were really complex, this would be a good route. However, because it's only comments, AJAX seems to be a bit overkill.
T. Stone
Well, you could code it in JavaScript (like Devi's answer), but then you might as well add it as an hidden element, which the OP doesn't want for some reason.
Kobi
I don't want it hidden because then I would have to hide it as many times as there are comments.
WillyG
+2  A: 

On link click create the form dynamically. e.g:

$('a.link').click(function()
{
   $('body').append("div id='dynamicdiv' class='y'><input type='text class='xyz' />" +
                    ".........................");
   $('#dynamicdiv').css({'position': 'absolute', 'top': '100px', 'left': '200px'});
});
Devi
@Devi - hello. Select the code block, and click on the code button (101010) to format it. Welcome to stack overflow.
Kobi
Sweet, I'll try this tomorrow..its getting late.
WillyG
A: 

I would add the form once, hide it, clone and append it when needed. The link would contain an id (or other identifier) which references the comment. Use javascript to manipulate the action attr of the form or the url option (if using the jQuery.form plugin).

czarchaic