Rahul Kumar Rahul Kumar - 4 months ago 11
Javascript Question

after passing event in function event.preventDefault() is not working

<div id="address" class="col s12">
<div class="row">
<form method="post" action="" id="addressDetails">
<div class="input-field col s6">
<textarea id="lAddress" name = 'lAddress' minlength='20' maxlength='100' class="materialize-textarea" class="validate" required length="100"></textarea>
<label for="lAddress" data-error="Must be between 20 to 100 Characters">Local Address</label>
</div>
<div class="input-field col s6">
<textarea id="pAddress" name = 'pAddress' minlength='20' maxlength='100' class="materialize-textarea" class="validate" required length="100"></textarea>
<label for="pAddress" data-error="Must be between 20 to 100 Characters">Permanent Address</label>
</div>
</form>
</div>
<div class="row center-align">
<button type="submit" name="submitAddress" form="addressDetails" class="waves-effect waves-light light-blue darken-1 btn updateProfile">Save Address Details</button>
</div>
</div>


HTML and JavaScript Code

$(document).ready(function() {
$("button").on('click', '.updateProfile', function(event) {
event.preventDefault();
});
});


This is my javascript. I simplified it keep it short. My
event.preventDefault()
is not working.

Answer

Based on your HTML markup <button> element already has class updateProfile, so your code should look like that:

$('button.updateProfile').on('click', function(e) {
    e.preventDefault();
});

In your case passing .updateProfile as a second argument of on method binds click event to the element with class updateProfile inside <button> element in a delegated way.