Raphael1
Raphael1

Reputation: 84

JQuery trigger event when page loads

I have a dropdown list and I want the first element of the list to trigger event automatically when the page loads.

My jquery current code:

<script type="text/javascript">
$(document).ready(function() {
    $(".size").change(function() {
        var searchbox = $(this).val();
        var myId = $(this).attr("id");
        var dataString = 'searchword=' + searchbox + '&product_id=' + myId;
        if (searchbox == '') {} else {
            $.ajax({
                type: "POST",
                url: "check_stock.php",
                data: dataString,
                cache: false,
                success: function(html) {
                    $("#display" + myId).html(html).show();
                }
            });
        }
        return false;
    });
});​
</script>

html code

<form id="form1" name="form1" method="post" action="cart.php">
    <label>Size:</label>
    <select name="size" class="size" id="'.$pid.'">

    <option value="XS">XS</option>
    <option value="S">S</option>
    <option value="M">M</option>
    <option value="L">L</option>
    <option value="XL">XL</option>
    <option value="XXL">XXL</option>
    <option value="3XL">3XL*</option>
    <option value="4XL">4XL*</option>
    <option value="5XL">5XL*</option>
    </select>
    <input type="hidden" name="pid" id="pid" class="pid" value="'.$pid.'" />
    <span id="display'.$pid.'"></span>
    </form>

Currently it only trigger when you select something from the dropdown list. (Any help appreciated.)

Upvotes: 1

Views: 1391

Answers (1)

19greg96
19greg96

Reputation: 2591

try adding this line after you add the listener:

$(".size").change();

this should fire your event on the dropdown list just as if someone selected an option.

Upvotes: 3

Related Questions