1

If you take a look on this post, you will see that there is a "add comment" link below it. When I click on it, a textarea will be added and allow user to write comment.

I am wondering how I can write a similar function?

Updated

Thank you everyone for your help.

I made a working code. However, if it is running with Google Chrome, I will get a terrible lag. I have to wait about 15 seconds to get the textarea out.

But if I run it with IE, then no problem at all... That is a very strange issue.

Here is my code for the JavaScript function

function testCall(id) {

var divName = "#Post-Number-" + id;

var appenedDiv = "<div class='container2' id='r1'>";
appenedDiv+="<div class='acc_container'>";
appenedDiv+=" <div class='block'>";
appenedDiv += "<form method='post' action='' id='loginForm' name='loginForm'>";
appenedDiv+="<p>My Reply:</p>";
appenedDiv+="<p>";
appenedDiv += "<textarea id='test' name='txtReply' cols='8' rows='10'></textarea>";
appenedDiv += id;
appenedDiv+="</p>";
appenedDiv+="<p>";
//appenedDiv+="<%= Html.ActionLink('Submit Reply', 'Login', 'User', new { returnUrl = Request.Url }, new { @class = 'linkButton' })%>";
appenedDiv+="</p>";
appenedDiv+="</form>";
appenedDiv+="</div>";
appenedDiv+="</div>";
appenedDiv += "</div>";

$(divName).after(appenedDiv);
$('html, body').animate({ scrollTop: $('#r1').offset().top }, 100);
//$('#test').focus();

}

Here is how I call this function

<a href="#" onclick="testCall(<%:item.QAID %>);">Test</a>
2
  • This does not require using jQuery or any other framework/library. Although, those can be helpful. Commented Jun 26, 2011 at 4:56
  • The reason I say that is because looking into a library will just make your life easier. You can do this in one line using jquery. Commented Jun 26, 2011 at 5:18

3 Answers 3

1

Simple example: http://jsfiddle.net/zBUgZ/

<a onclick="addComment(this)">add comment</a>

function addComment(el){
    var t = document.createElement('textarea');
    document.body.appendChild(t);
    el.style.display = 'none';
}

Using jQuery: http://jsfiddle.net/zBUgZ/1/

<a class="addcomment">add comment</a>

$('.addcomment').click(function(){
    $(this).after('<textarea class="comment"></textarea>').hide();
});

EDIT

Since it's more likely that the ANCHOR will have a HREF attribute, you should also return false:

<a class="addcomment" href="http://somelink.somewhere.com">add comment</a>

$('.addcomment').click(function(){
    $(this).after('<textarea class="comment"></textarea>').hide();
    return false;
});

And if you want to set an ID on the TEXTAREA, you might use one based on the ANCHOR:

<a id="addcomment1" class="addcomment" href="http://somelink.somewhere.com">add comment</a>

$('.addcomment').click(function(){
    $(this).after('<textarea class="comment" id="'+this.id+'-textarea"></textarea>').hide();
    return false;
});

http://jsfiddle.net/zBUgZ/2/

EDIT 2

And, noting that you're trying to focus on the element, you can do this if you add an ID to the TEXTAREA:

Plain Javascript:

document.getElementById(this.id+'-textarea').focus();

jQuery:

$('#'+this.id+'-textarea').trigger('focus');

http://jsfiddle.net/zBUgZ/4/

Sign up to request clarification or add additional context in comments.

2 Comments

hi Jared Farrish, what if I have several links and have different ID? what should I do with the onclick event?
You mean the links have the ID? I would use a class to add the event handler. What do you want to do with the ID?
1

You can call a jQuery function on the click of "Add Comment" link. In the function, you can do two things:

  1. Show a hidden div that contains the textarea.
  2. Append a textarea to the calling div to the innerHTML property.

5 Comments

I was trying the second method. I append the textarea to the end of my current div. It is not inside current div, but right under it. The text area shows, but I lost the focus. The page goes all the way to the top, but it supposes to set focus on the text area...
just append the textarea with an id and after appending set the focus to the id..hope that helps
Can't you try the 1st option..I generally use the 1st one...its sort of very easy to use.
the first one is easy.. but I am actually building a forum style project. If there is 100 reply, then I have to use 100 hidden div, which will be crazy...
You can use the "Edit 2" provided by Jared above..Thats the way you can set the focus.
1

I suppose some validation would be in order to make this identical to the stack overflow addComment box.

http://jsfiddle.net/CKzsn/

HTML

<div id="container">
    <a href="#" title="Add comment" class='addComment'>Add Comment</a>
    <div class="hide">
        <textarea id="commentBox"></textarea>
        <button> Submit </button>
    </div>
</div>

Jquery

$('.addComment').click(function(e) {
    e.preventDefault();
    $(this).fadeOut();
    $(this).next('div').slideToggle();   

}); 

$('button').click(function() {
    $('.hide').fadeOut();
    var text = $('#commentBox').val();
    $('.addComment').fadeIn();
    $('<div class="comment">' + text + '</div>').insertBefore('.addComment');

});

4 Comments

Your's "will actually function" eh?
of course it needs some method to be saved server side. I think that is rather obvious. But yes. this will function.
"difference between mine and the accepted answer is that mine will actually function." I guess you could explain what you mean if you're going to say things like that. ;)
I guess, that could come off a bit improper. I'll remove that for you =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.