Problem in my Jquery code

0

Personal this is my code in PHP

$sql = mysql_query("SELECT * FROM noty");
while($linha = mysql_fetch_array($sql)){
    $notificacao = $linha["notificacao"];

echo"<div class='adm_edit'>".$notificacao."
   <input type='text' class='input_edit' value='".$notificacao."'/>
</div>";
}

As you can see I'm getting the information from the bank and showing. So far so good, but the problem comes in JQuery .

$(document).ready(function(){

$(".adm_edit").on("click", function() {
    $(this).children(".input_edit").show();
    $("#exit_edit").show();
});

$("#exit_edit").click(function() {
    $(".adm_edit").text($(".input_edit").val());
    $(".input_edit").hide();
    $("#exit_edit").hide();
});
});

Each content that is generated by the bank as you can see has an invisible Input that is displayed when the user clicks on the content. The problem comes after that which is when I would like when the Input when it was closed the contents typed there would be the Div that was clicked.

    
asked by anonymous 02.02.2016 / 20:00

1 answer

0

Igor, try to store current .adm_edit and .input_edit in some broader scope variable.

$(document).ready(function(){
  (function () {    
    var atual = {};
    var inputs = {};

    inputs.exitEdit = $("#exit_edit");
    inputs.admEdit = $(".adm_edit");    
    inputs.admEdit.on("click", function() {
      atual.admEdit = $(this);
      atual.inputEdit = atual.admEdit.children(".input_edit");

      atual.inputEdit.show();
      inputs.exitEdit.show();
    });

    inputs.exitEdit.click(function() {
      atual.admEdit.text(atual.inputEdit.val());
      atual.inputEdit.hide();
      inputs.exitEdit.hide();
    });
  })():
});

Note that I am storing the div.adm_edit cliac and its input.adm_edit in a variable ... I did this within a closure to avoid future scope problems.

    
02.02.2016 / 20:58