37

I have a function that on change event run the post actions.

$("select#marca").change(function(){
    var marca = $("select#marca option:selected").attr('value');
    $("select#modello").html(attendere);
$.post("select.php", {id_marca:marca}, function(data){
        $("select#modello").html(data);
    });
});

I would like to perform this function onload event. Is it possible? Is there a good way to do this?

1
  • You mean trigger the function on load? have you tryed: $("select#marca").change(); Commented Feb 20, 2013 at 16:15

6 Answers 6

64

Just put it in a function, then call it on document ready too, like so:

$(function () {
    yourFunction(); //this calls it on load
    $("select#marca").change(yourFunction);
});

function yourFunction() {
    var marca = $("select#marca option:selected").attr('value');
    $("select#modello").html(attendere);
    $.post("select.php", {id_marca:marca}, function(data){
        $("select#modello").html(data);
    });
}

Or just invoke change on page load?

$(function () {
    $("select#marca").change();
});
Sign up to request clarification or add additional context in comments.

2 Comments

I tried $(window).load(function () { $(function () { $("select#marca").change(); }); but the event not run..
@PaoloRossi $(function() will already execute on page load don't wrap it inside a $(window).load and make sure you have registered the change event before trying to fire .change
47

Really simple way it to just chain another .change() event to the end of your on change function like this:

$("#yourElement").change(function(){
   // your code here
}).change(); // automatically execute the on change function you just wrote

Comments

18

If you add .change() to the end it will be called straight away after being bound:

$(function() { 
    $("select#marca").change(function(){
        var marca = $("select#marca option:selected").attr('value');
        $("select#modello").html(attendere);
    $.post("select.php", {id_marca:marca}, function(data){
            $("select#modello").html(data);
        });
    }).change(); // Add .change() here
});

Or change the callback to an actual function and call that:

function marcaChange(){
    var marca = $("select#marca option:selected").attr('value');
    $("select#modello").html(attendere);
$.post("select.php", {id_marca:marca}, function(data){
        $("select#modello").html(data);
    });
}

$(function() { 
    $("select#marca").change(marcaChange);
    marcaChange();
});

Comments

4

this is working for me.

 $(function () {
    $('#checkboxId').on('change', hideShowfunction).trigger('change');
});

function hideShowfunction(){
//handle your checkbox check/uncheck logic here
}

Comments

2

To call onload, you can try jQuery:

 $(document).ready(function(){
 onchange();// onload it will call the function
     });

Write your onchange function like this, so this will be called when onchange occurs,

function onchange(){
    var marca = $("select#marca option:selected").attr('value');
    $("select#modello").html(attendere);
    $.post("select.php", {id_marca:marca}, function(data){
    $("select#modello").html(data);
});

Comments

1

The other way to do

 $("select#marca").val('your_value').trigger('change');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.