aniogs
aniogs

Reputation: 15

Jquery click event not working after change event

Here is my code:

 function change() {
        console.log("change");
        $('span[data-valmsg-for="Car"]').text('');
    }

$("#saveCar").on("click", function () {
            console.log("savecar");
            if (IsCarFormValid() == true) {
                $("#saveCar").attr("disabled", true);
                var model = {};

            }
        });

When I change data in input and click on button the button click event doesn't work. Why?

Upvotes: 0

Views: 238

Answers (1)

johannchopin
johannchopin

Reputation: 14844

Using an alert() and you will see that the event is working fine:

$("#Car").on("change", function() {
  console.log("vincode change");
  $('span[data-valmsg-for="Car"]').text('');
});

$("#saveCar").on("click", function() {
  alert("button is clicked")
  console.log("savecar");
  var model = {};
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" class="form-control" id="Car" name="Car" value="ggdsgdsagdgag">
<span data-valmsg-for="Car" class="text-danger">some validation error</span>

<button type="button" class="btn btn-success" id="saveCar">Save</button>

What can be a little confusing is that the sentence some validation error is deleted after an onBlur event (when the mouse click outside of the input). So if you change your text and click directly on the button, the text will first disappear that make that your cursor is no more on the button. So in practice, you don't click on the button.

But if you change the sentence, click outside of the input (not on the button) and then on the button, you will see the alert message.

Upvotes: 1

Related Questions