2

I have checked How to get ID of button user just clicked? this question before asking.

I am trying to get the id of button. Below are the codes.

<form name="edit_posting1" id="edit_posting1" method="post" action="edit_posting_bk.php" role="form">

<center>

<input class="btn btn-home" type="submit" name="publish" id="publish" alt="Publish" value="Preview"/>
<input class="btn btn-home" type="submit" name="save" id="save" onclick="return confirm('Are you sure you want to Submit.')"  alt="Save" value="Save"/>
<?php
if($disable == true){
    echo '<input class="btn btn-home" type="submit" name="disable" id="disable" alt="Disable" value="Disable"/>';
}else{
    echo '<input class="btn btn-home" type="submit" name="enable" id="enable" alt="Enable" value="Enable"/>';
}
?>
</center>

Ajax Code

<script type="text/javascript">
    $("#edit_posting1").on("submit", function(e) {
        e.preventDefault;
        var btn = $('#publish');
        //var el = 'publish';
        var el = $(btn).attr('id');
        alert(el);
    })
</script>

As per my understanding issue with Ajax code. Can somebody advise me on this?

Thanks

Note: What i want is if user click on enable/disable or publish button i get the id or name of the button.

Community
  • 1
  • 1
Roxx
  • 3,738
  • 20
  • 92
  • 155

3 Answers3

2

If you want to get the id of pressed submit button on form submit you can do it by listening which one was clicked, save it's id in variable, and then access that variable in form submit event handler:
Check the below snippet

 var subm = "";
 $('input[type="submit"]').click(function(e) {
   subm = e.target.id;
 });


 $("#edit_posting1").on("submit", function(e) {
   e.preventDefault();
   var el = subm;
   alert(el);
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="edit_posting1" id="edit_posting1" method="post" action="edit_posting_bk.php" role="form">

  <center>

    <input class="btn btn-home" type="submit" name="publish" id="publish" alt="Publish" value="Preview" />
    <input class="btn btn-home" type="submit" name="save" id="save" onclick="return confirm('Are you sure you want to Submit.')" alt="Save" value="Save" />
    <?php if($disable==t rue){ echo '<input class="btn btn-home" type="submit" name="disable" id="disable" alt="Disable" value="Disable"/>'; }else{ echo '<input class="btn btn-home" type="submit" name="enable" id="enable" alt="Enable" value="Enable"/>'; }
    ?>
  </center>
The Process
  • 5,913
  • 3
  • 30
  • 41
1

Try not to pollute global scope instead of that use .data(),

 $("#edit_posting1").on("submit", function(e) {
   e.preventDefault();
   var el = $(this).data('submitter');
   alert(el);
 });

 $("#publish,#save").click(function(e) {
   if (this.id == "save") {
     if (!confirm('Are you sure you want to Submit.')) {
       e.preventDefault();
       return false;
     }
   }
   $(this).closest("#edit_posting1").data("submitter", this.id);
 });

DEMO

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

Try this way -

<script type="text/javascript">
    $(function () {
        $('.btn-home').on('click', function (e) {
            e.preventDefault();
            var id = $(this).attr('id');
            alert(id);
        });
    });
</script>
d-coder
  • 1,180
  • 1
  • 11
  • 17
  • Thanks for your answer. Voted up. – Roxx Mar 12 '16 at 19:11
  • 1
    @CalculatingMachine This is not the submit event though. It's the click. Is that ok? This fires before submit, so you can make data from the click event available to the submit if you want. – dewd Mar 12 '16 at 19:13