1

This is a comment script for my website but some part of the jquery/ javascript code does not seem to work correctly.

When editing a comment instead of posting it to php I have made a small notification window to test if it is actualy changing the value passed through.

Onclick will take the current comment and create a textarea with that comment in it.

Now if a users starts changing the text and he/she hits the EDIT button, the small testing notification still shows the original comment even if the text is totally diffrent.

Pleas help.

this is the code

$("a.edit").click(function(){
      var eid = $(this).data("id");
      var econtent = $("li[data-id='" + eid + "'] .comment-cont").text();
      $("li[data-id='" + eid + "'] .comment-cont").html('<div class="edit-error"></div><form><textarea class="edit-comment">'+econtent+'</textarea></form><a  href="javascript:;" onclick="notify('+econtent+')" class="editcomment button_yellow">Edit</a>');
      $(".edit-comment").keyup(function(){
            var content = $('.edit-comment').val();
            if (content.length < 3){
                $(".edit-error").show();
                $('.edit-error').html('Content can not be shorter then 3 characters');
                $("a.editcomment").hide();
            }else if(content.length < 300){
                $(".edit-error").hide();
                $('.edit-error').html('')
                $("a.editcomment").show();
            }else{
                $(".edit-error").show();
                $('.edit-error').html('Content can not be longer then 300 characters');
                $("a.editcomment").hide();
            }
    });
    $("a.editcomment").click(function(){
        var edited = $('.edit-comment').text();
        notify(edited);
    });
});

So if a user clicks the edit button his/her comment will go into a textarea with a EDIT button.

PS : The functions

notify() = small notification box.

var eid = comment id.

var econtent = the original content

var edited = the edited content ( that still is the original content ).

Community
  • 1
  • 1
rokkz
  • 179
  • 1
  • 1
  • 13

1 Answers1

0

I think you just need to change $('.edit-comment').text(); to $('.edit-comment').val();

$("a.editcomment").click(function(){
    var edited = $('.edit-comment').val();
    notify(edited);
});

It seems that val() is the preferred way of getting text from a textarea.

Community
  • 1
  • 1
jstricker
  • 2,132
  • 1
  • 30
  • 44