Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery .on() method with load event

Tags:

jquery

events

How can we use jQuery .on() method with load event? e.g. The code is:

<a href="#" id="add">Add</a> <div id="initial">      <input type="text" class="abc" name="in"> </div> 

And the jQuery for it:

jQuery(document).ready(function() {     var x=$('#initial').html();     $('#add').click(function(){         $('body').append(x);     });     $(document).on('load','.abc',function(){         alert('started');     }); }); 
like image 675
Anand mohan sinha Avatar asked Jul 11 '12 03:07

Anand mohan sinha


People also ask

How do I ensure jQuery is loaded?

First, you need to make sure that jQuery is included before any scripts that use it. This can be tricky if you have javascript embedded in partials. If you load jQuery in the header and all of your other script in a script block at the end of the body, you can be sure that jQuery is loaded before your other code.

What is Load event in jQuery?

The load event occurs when a specified element has been loaded. This event works with elements associated with a URL (image, script, frame, iframe), and the window object. Depending on the browser, the load event may not trigger if the image is cached (Firefox and IE).

What is the use of load () function?

The Load function initializes a database and loads it from an input data file. It can be used for initial loading of a database, as part of a database reorganization, or for reloading a database after changing the DBD definition.

How can call Click event on page load in jQuery?

Yeah, you can use a click event called onLoad() . Just use the setTimeout() method in jquery. It will call a click function without clicking it.


1 Answers

Refer to http://api.jquery.com/on/

It says

In all browsers, the load, scroll, and error events (e.g., on an <img> element) do not bubble. In Internet Explorer 8 and lower, the paste and reset events do not bubble. Such events are not supported for use with delegation, but they can be used when the event handler is directly attached to the element generating the event.

If you want to do something when a new input box is added then you can simply write the code after appending it.

$('#add').click(function(){         $('body').append(x);         // Your code can be here     }); 

And if you want the same code execute when the first input box within the document is loaded then you can write a function and call it in both places i.e. $('#add').click and document's ready event

like image 129
Imdad Avatar answered Sep 23 '22 16:09

Imdad