php - JQuery adds unwanted div after an ajax call -


i have jquery ajax call add new comment database. saves fine when want reaload div comments shown, additional div not want. when reaload page fine , displayed wanted!

the script:

<script>         $("#addcmt").click(function() {             var userid = $("#userid").val();             var classid = $("#classid").val();             var text = $("#appendedinputbutton").val();             var option;             if($("#option").is(':checked')) {                 option = 3;             } else {                  option = 1;             }             $.ajax({                 type: "get",                 url: "/comment/functions/add_class_comment.php",                 data: "text=" + text + "&userid=" + userid + "&classid=" + classid + "&option=" + option,                 success: function(msg) {                     $("#commentlist").load(location.href+' #commentlist');                     $('#appendedinputbutton').val("");                     $('.criticalcomment').attr('checked', false);                 }             });         });     </script> 

the php+html comments shown:

<div class="bs-docs-example" id="message">     <div id="commentlist">         <?php             ($i=0; $i<$l; $i++) {                 switch($commentarr[$i]->getoption()) {                     case 1:                         $option="alert-success";                         break;                     case 2:                         $option="alert-info";                         break;                     case 3:                         $option="alert-error";                 }                 echo '<div class="comments alert '.$option.'"><div class="commentsname">'.$userarr[$i]->getfirstname().' '.$userarr[$i]->getlastname().'</div>';                 echo '<div class="commentsdate">'.renderdate($commentarr[$i]->getdate()).'</div><div class="commentstext">'.$commentarr[$i]->gettext().'</div>';                 if ($deletebutton == 1) {                     echo '<div class="deletebuttonadmin"><input type="button" class="btn btn-small btn-primary delcmt" value="l&ouml;schen" name="'.$commentarr[$i]->getid().'"></div>';                 }                 echo '</div>';             }         ?>     </div>     <form class="comments postmessage">         <div class="input-append" style="margin-top: 10px;">             <input type="hidden" name="classid" id="classid" value="<?php echo $c->getid(); ?>">             <input type="hidden" name="userid" id="userid" value="<?php echo $u->getid(); ?>">             <textarea class="span12" name="text" id="appendedinputbutton" type="text"></textarea>             <label for="option">kritisch?</label><input type="checkbox" id="option" class="criticalcomment" name="option" value="1">             <input type="button" class="btn" id="addcmt" value="post">         </div>     </form> </div> 

i hope got idea or hint me!

well, $("#commentlist").load(location.href+' #commentlist'); tells jquery replace contents of #commentlist #commentlist, 2 nested #commentlists, correct?

you might have $("#commentlist").remove().load(location.href+' #commentlist'). if not work, try introducing temporary div:

$("<div />").load(location.href+' #commentlist', function (div) {     $('#commentlist').replacewith($(div)); }) 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -