0

function deletePost(id) {
  var db_id = id.replace("post_", "");
  // Run Ajax request here to delete post from database
  document.body.removeChild(document.getElementById(id));
}

function CustomConfirmPop() {
  this.render = function(dialog, op, id) {
    var winW = window.innerWidth;
    var winH = window.innerHeight;
    var dialogoverlay = document.getElementById('dialogoverlay');
    var dialogbox = document.getElementById('dialogbox');
    dialogoverlay.style.display = "block";
    dialogoverlay.style.height = winH + "px";
    dialogbox.style.left = (winW / 2) - (550 * .5) + "px";
    dialogbox.style.top = "100px";
    dialogbox.style.display = "block";

    document.getElementById('dialogboxhead').innerHTML = "Confirm that action";
    document.getElementById('dialogboxbody').innerHTML = dialog;
    document.getElementById('dialogboxfoot').innerHTML = '<button type="button" onclick="Confirm.yes(\'' + op + '\',\'' + id + '\')">Yes</button> <button onclick="Confirm.no()">No</button>';
  }
  this.no = function() {
    document.getElementById('dialogbox').style.display = "none";
    document.getElementById('dialogoverlay').style.display = "none";
  }
  this.yes = function(op, id) {
    if (op == "delete_post") {
      deletePost(id);
    }
    document.getElementById('dialogbox').style.display = "none";
    document.getElementById('dialogoverlay').style.display = "none";
  }
}
var Confirm = new CustomConfirmPop();
<div id="dialogoverlay"></div>
<div id="dialogbox">
  <div>
    <div id="dialogboxhead"></div>
    <div id="dialogboxbody"></div>
    <div id="dialogboxfoot"></div>
  </div>
</div>
<p id="post_1">
  Today is a lovely day ...
  <button onclick="Confirm.render('Delete Post?','delete_post','post_1')">Delete</button>
</p>


<script>
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text

    var r = confirm("You Want to Remove??");
    if (r == true) {
    e.preventDefault(); $(this).parent('div').remove(); x--;
    }
});
});
//how can i use this code and apply my css styling to it becuase this works to some extent?
</script>

ok so when i click delete there should be a confirmation pop up box asking yes or no before deleting the post. The problem is when i click on delete it just select yes off its own without me getting the chance to click it or not. I wonder if its conflicting with the other buttons in my html page.

Lee
  • 25
  • 6
  • It works fine for me. In your question you were missing the `<` in `

    `. I corrected that when making the stack snippet, is that error in the original page?

    – Barmar Aug 22 '16 at 07:03
  • i am trying to put it on a dynamic generated input fields using jquery. To confirm if the user wants to delete the field – Lee Aug 22 '16 at 07:39
  • See http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements – Barmar Aug 22 '16 at 07:40

4 Answers4

1

Actually your logic is working correctly, the problem with your code is that you're closing the #dialogoverlay immediately, while the #dialogbox is outside, you should nest it inside and that's it because it was pushing both the post as well as the #dialogbox down since the overlay height is same as full window height

codePen

HTML:

<div id="dialogoverlay">
  <div id="dialogbox">
    <div>
      <div id="dialogboxhead"></div>
      <div id="dialogboxbody"></div>
      <div id="dialogboxfoot"></div>
    </div>
  </div>
</div>
<p id="post_1">
  Today is a lovely day ...
  <button onclick="Confirm.render('Delete Post?','delete_post','post_1')">Delete</button>
</p>
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
  • 1
    Put your code here in a stack snippet instead of codepen – Barmar Aug 22 '16 at 07:04
  • @Barmar, thank you and it's just I was still writing – Mi-Creativity Aug 22 '16 at 07:09
  • its not working i tried everything but it keeps choosing yes when i load it i dont know why. – Lee Aug 22 '16 at 07:15
  • it is working in the codepen I provided in the answer, if you click "no" the post will *not* be deleted, please make sure you have updated your html code as shown in my answer – Mi-Creativity Aug 22 '16 at 07:18
  • in the one here you change button id to btn but i am not seeing the link in codepen – Lee Aug 22 '16 at 07:20
  • sorry about the button id it was for something else, I fixed it, the codepen link is in the question the blue text right above the code snippet, same as this one http://codepen.io/Mi_Creativity/pen/WxBAwP – Mi-Creativity Aug 22 '16 at 07:23
  • here's another codepen: http://codepen.io/Mi_Creativity/pen/AXkErE same code as above but with some basic css style – Mi-Creativity Aug 22 '16 at 07:28
  • well, if your goal is to display the confirmation box when the user click on the delete button, and if the user clicks on "no" it won't be deleted, and if the user clicks on "yes" it will be deleted, then it is working just like it should, I even have tested it with more than one `p` "post" element and it works properly.. but if you're trying to do something else other than that then I'm sorry I am not getting your point – Mi-Creativity Aug 22 '16 at 07:56
0

The code work to me. But the confirmation message appear at the end of the page.

Try to change

dialogoverlay.style.height = winH+"px";

with

dialogoverlay.style.height = "50px";

and you will see it works.

Alberto
  • 71
  • 1
  • 7
0

Your code is working fine in the snippet,the problem is with the CSS you need to use position:absolute for "dialogoverlay" and "dialogbox".

0
<button type="button" class="dlt" >Delete</button>

Now use jquery to getAlert

$(".dlt").click(function(){

var r = confirm("You Want to Remove??");
        if (r == true) {
// your code.
}
});
DBB
  • 137
  • 1
  • 4
  • I used you code and its working fine but i wanted to use my custom code for this – Lee Aug 22 '16 at 07:36