Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run jQuery function and on load and on change?

I want to run a script on page load first time (for default value of select element), and then on each change of select element. Is it possible to do it in one line, or do I need to duplicate function for this events?

I know that I can use next structure:

$('#element').on('keyup keypress blur change', function() {
    ...
});

But it doesn't seem to support load event.

Any ideas?

like image 387
Braggae Avatar asked Jul 29 '13 11:07

Braggae


People also ask

What is the use of jQuery load method?

jQuery load () method is simple but very powerful AJAX method. The Load () method in jQuery helps to load data from server and returned into selected element without loading the whole page. Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.

How to load data from server using Ajax in jQuery?

jQuery load() method is simple but very powerful AJAX method. The Load() method in jQuery helps to load data from server and returned into selected element without loading the whole page. Syntax: $(selector).load(URL, data, callback); Parameters: This method accepts three parameter as mentioned above and described below:

What is the difference between jQuery click () and jQuery ready ()?

The $(document).ready() method allows us to execute a function when the document is fully loaded. This event is already explained in the jQuery Syntax chapter. The click() method attaches an event handler function to an HTML element. The function is executed when the user clicks on the HTML element.

How to execute a function from a DOM event in jQuery?

In jQuery, most DOM events have an equivalent jQuery method. The next step is to define what should happen when the event fires. You must pass a function to the event: // action goes here!! The $ (document).ready () method allows us to execute a function when the document is fully loaded.


3 Answers

If i understand what you mean, just trigger any one of these events:

$('#element').on('keyup keypress blur change', function() {
    ...
}).triggerHandler('keyup'); //or trigger('keyup') or keyup() which then let event propagate
like image 200
A. Wolff Avatar answered Oct 18 '22 18:10

A. Wolff


for change element use your :

$('#element').on('change', function() {
    ...
});

and to use same function on load first time use structure like:

jQuery(window).on("load", function(){

  $('#element').change();

});

to call the same function

like image 42
Ivan Pavić Avatar answered Oct 18 '22 19:10

Ivan Pavić


Instead of using anonymous functions (defining them right where they will be used) you could just create a regular function and tell jQuery to call it when those events happen.

$(document).ready(myFunction);
$('#element').on('keyup keypress blur change', myFunction);

function myFunction() {
    // Do something here...
}
like image 29
Ambroos Avatar answered Oct 18 '22 20:10

Ambroos