I have a page that has a button on it. When the user clicks the button it dynamically render's a form (ie it's not just showing a hidden form.. it's completely creating it using jQuery).
My issue is the newly created form doesn't respond to any jQuery commands. This is the code I have for the rendered form at the moment.
$("#savenewlang").click(function(e) {
console.log("savenewlang has been clicked");
});
So it should just console.log when they click the submit button but it's not running.
Any idea how to reload the DOM or assign that an actual event that correctly fires?
The location. reload() method reloads the current URL, like the Refresh button.
You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.
On Refresh/Reload/F5: If user will refresh the page, first window. onbeforeunload will fire with IsRefresh value = "Close" and then window. onload will fire with IsRefresh value = "Load", so now you can determine at last that your page is refreshing.
$("#container").on('click', '#savenewlang', function(e) {
console.log("savenewlang has been clicked");
});
Here #container
points to a parent element of #savenewlang
that belongs to DOM at page load.
To specify this event .on()
you need three arguments
.on(eventName, target, callback);
But for ordinary binding it only needs two arguments
.on(eventName, callback);
Read more about .on()
Put all of your code within
$(document).ready(function() {
});
in short
$(function() {
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With