Richa
Richa

Reputation: 3289

Two onclick event

I have onclick two times on same button, button for some the second one does not seem to work. What do i need to do inorder to make both of them work.

<button type="submit" style="display:none;" id="done">Ok</button>

1st event

$(document).ready(function () {
  $('#done').click(function () {

  });
});

2nd event

$(function () {
  $(document).on("click", "#done", Done);
});



function Done() {
  $.ajax({

  });
}

Upvotes: 1

Views: 149

Answers (2)

codefreaK
codefreaK

Reputation: 3662

Check the Demo It works

Check the second demo it shows the defualt case ie when it works without error

Depending on the status of the request output is shown here the output is rejected hence enters the fail case

Jquery

$(document).ready(function () {

    $('#show').click(function(){
         $('#done').show();
    });
  $('#done').click(function () {
      alert('called 1st');
    $(this).siblings('.col-lg-11').attr('contenteditable', 'false');
    $(this).siblings('.col-lg-11').attr('style', 'border:none;');
    $(this).attr('style', 'display:none;');
    $(this).siblings('.edit-link').attr('style', 'display:li;');
    $(this).siblings('.cancel-link').attr('style', 'display:none;');
  });
});
$(function () {
  $(document).on("click", "#done", Done);
});



function Done() {
    alert('called 2nd');
    var s="sa";  

var request = $.ajax({
  type: "POST",
    url: "http://www.google.co.in",
  data: { name: "John", location: "Boston" }
}).done(function(  ) {
    alert( "Data Saved: "  );

  });

request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});    

}

Upvotes: 2

Husman
Husman

Reputation: 6909

I believe you need to debug the issue a little bit. The title of the question indicates that javascript (or jQuery) is not handling the click event. This may not be the case.

$(document).ready(function () {
    $('#done').click(function () {
        console.log('first')
    });

});

$(function () {
    $(document).on("click", "#done", Done);
});

function Done() {
    console.log('second')
}

<button type="submit" style="display:block;" id="done">Ok</button>

This runs fine, see the jsfiddle, the console in my browser logs both first and second messages. So it looks like both events are firing.

You now need to debug your ajax request or your controller. Try getting a simple file (single string within it) and alerting it. Then you can pinpoint your exact problem.

Upvotes: 2

Related Questions