0

I have a problem with jquery ajax. When i add a new element with ajax, i can't do anything with that. here is my code:

$(".yorum_input").keypress(function(e) {

  if (e.keyCode == 13) {
    e.preventDefault();
    var alt_id = $(this).attr('yorum_id');
    var val = $(this).val();
    var yazi = $(this).attr('yazi_id');
    $(this).val("");

    $.ajax({
      url: url,
      type: "post",
      data: {
        "alt_id": alt_id,
        "icerik": val,
        "yazi": yazi
      },
      success: function(cevap) {
        console.log(cevap);
        cevap1 = cevap.split('***');
        if (cevap1[0] == "succ") {
          if (cevap1[1] == "1") {
            if (cevap1[8] == "0") {
              $("#yorumlar").append("<div class='box-comment' yorum_id='" + cevap1[2] + "'>" +
                "<!-- User image -->" +
                "<a href='" + cevap1[3] + "'>" +
                "<img class='img-circle img-sm' src='" + cevap1[4] + "' alt='User Image'>" +
                "</a>" +
                "<div class='comment-text'>" +
                "<span class='username'>" +
                "<a href='" + cevap1[3] + "'>" + cevap1[5] + "</a>" +
                "<span class='text-muted pull-right'>" + cevap1[6] + "</span>" +
                "</span><!-- /.username -->" +
                cevap1[7] +
                "</div>" +
                "<!-- /.comment-text -->" +
                "</div>" +
                "<div class='alt_yorumlar' id='" + cevap1[2] + "'></div><div class='box-comment' style='margin-left:50px;'>" +
                "<img class='img-responsive img-circle img-sm' src='" + cevap1[4] + "' alt='Alt Text'>" +
                "<!-- .img-push is used to add margin to elements next to floating images -->" +
                "<div class='img-push'>" +
                "<input type='text' yorum_id='" + cevap1[2] + "'  yazi_id='" + cevap1[9] + "' class='form-control input-sm yorum_input' placeholder='Yorumunuzu yazın!'>" +
                "</div></div>");
            } else {
              $("div[class='alt_yorumlar'][id='" + cevap1[8] + "']").append("<div class='box-comment' style='margin-left:50px;' yorum_id='" + cevap1[2] + "'>" +
                "<!-- User image -->" +
                "<a href='" + cevap1[3] + "'>" +
                "<img class='img-circle img-sm' src='" + cevap1[4] + "' alt='User Image'>" +
                "</a>" +
                "<div class='comment-text'>" +
                "<span class='username'>" +
                "<a href='" + cevap1[3] + "'>" + cevap1[5] + "</a>" +
                "<span class='text-muted pull-right'>" + cevap1[6] + "</span>" +
                "</span><!-- /.username -->" +
                cevap1[7] +
                "</div>" +
                "<!-- /.comment-text -->" +
                "</div>");
            }
          }
        }
      }
    });
  }
});

When I post a new comment, ajax goes to my php code and if the comment can succesfully added to my database, it returns the needed values for that comment. And then, I create a new DOM element includes an input. but I can't select that new input. Please somebody help me :D I need that for my Website's comments.

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

i suggest you to use Jquery template to render your HTML after success callback, so that you will avoid writing ton of HTML code inside your javascript file and you can also maintain it very easily.

follow this link for documentation:

http://stephenwalther.com/archive/2010/11/30/an-introduction-to-jquery-templates