nomen
nomen

Reputation: 3715

jQuery click() not getting triggered in Firefox 24

I'm using using jQuery to make some Ajax calls when a button is pressed. This is working perfectly in Chrome, but clicking the button in Firefox just reloads the page.

<button class="form-control btn btn-primary" id="qa-next">Next</button>
// using knockout to dynamically inject values in the page.
// works when triggered manually
$('#qa-next').click(function () {
  $.post("@{QACardR}", ko.mapping.toJSON(viewModel), function(newQA) {
    ko.mapping.fromJS(newQA, {}, viewModel);
  });
});

The

 $.post("@{QACardR}", ko.mapping.toJSON(viewModel), function(newQA) {
    ko.mapping.fromJS(newQA, {}, viewModel);
 });

works correctly if I run it in the console. But clicking the button just refreshes the page. So I can only conclude that the problem is the selector or click not working in firefox. What am I doing wrong?

Upvotes: 0

Views: 162

Answers (1)

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

Use e.preventDefault() inside your click function

$('#qa-next').click(function () {
  e.preventDefault();
  $.post("@{QACardR}", ko.mapping.toJSON(viewModel), function(newQA) {
    ko.mapping.fromJS(newQA, {}, viewModel);
  });
});

Upvotes: 1

Related Questions