Problem in my Jquery code

Asked

Viewed 53 times

0

Guys, this is my code on 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 taking the bank information and showing. So far so good, but the problem comes in the 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 a Input invisible that is shown when the user clicks on the content. The problem comes after that which is when I would like when the Inputwhen the contents typed there were closed Div that was clicked.

  • where’s this "#exit_edit" in the php file? it’s really hidden?

  • "as you can see has an invisible Input", sorry. But I did not understand that phrase, I entered a paradox.

  • I was going to try to help, but I also found it confusing. Maybe someone can understand and help, but suddenly it’s better to try to clarify. As I do not have a visual notion of the elements it was a little difficult for me to understand what you call closing input and also that div would be the one that would have a content to be put in the input.

  • @Diegodesouzasilva I ended up not putting the code of "Exit.php" but it is a div with "display:None" that serves to hide the input when clicked. If you want to see the code here is "<div id="exit_edit" ></div>"

  • @Zoom sorry is my first time posting here and I should have detailed more, but on your question the input is with "display:None" and is visible when it is clicked the div ". adm_edit"

  • @joaoPaulo sorry my first posting, but thanks for trying to help hehe :). Next time I will try to clarify better.

Show 1 more comment

1 answer

0

Igor, try to store the .adm_edit and the .input_edit current in some variable with wider scope.

$(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 cliacado and its respescesctivo input.adm_edit in a variable... I made it inside a closure to avoid future scope problems.

  • Thank you so much worked out the way I wanted to :) and I’ll start trying to use on what you talked about storing variables

Browser other questions tagged

You are not signed in. Login or sign up in order to post.