Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery reload the DOM?

Tags:

jquery

dom

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?

like image 461
Peter Avatar asked Jun 04 '12 16:06

Peter


People also ask

What is location reload ()?

The location. reload() method reloads the current URL, like the Refresh button.

How do you refresh a JavaScript window?

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.

How can I tell if jquery is refreshing a page?

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.


1 Answers

$("#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()


Remainder

Put all of your code within

$(document).ready(function() {

});

in short

$(function() {

});
like image 90
thecodeparadox Avatar answered Oct 27 '22 11:10

thecodeparadox