Skip to content Skip to sidebar Skip to footer

Apply The Checked Event When Loading In Jquery

I made a small change to the code at http://jsfiddle.net/daveSalomon/bcbxftr6/1/ to display the checked event by default: HTML Code:

Solution 1:

Not tested but may be loading the script whiting the $( document ).ready() function will do the magic :D

Solution 2:

All you need to do is trigger the change event after you create the event handler

$('.notice').change(function () {
    // your event handling code
     ......

   /* now trigger the event */
}).change();

DEMO

Solution 3:

One option is to just run the function once without conditions, like this....

var oldHtml = $('ol').html();
var reversed = false;

  var changefunc = function () {
    if ($(this).is(':checked')) {
    $.each($('li.top').get().reverse(), function(index, value) {
      $(value).insertBefore($('li')[0]);
    });
    reversed = true;
    $('li.top').show();
    return;
  }
  $('.notice').change(changefunc); 
  changefunc(); // Just run it once....

  $('ul').html(oldHtml);
  reversed = false;
  $('li.top').hide();
});

Solution 4:

Try triggering .change() event at page load

var oldHtml = $('ol').html();
var reversed = false;

  $('.notice').change(function () {
    if ($(this).is(':checked')) {
    $.each($('li.top').get().reverse(), function(index, value) {
      $(value).insertBefore($('li')[0]);
    });
    reversed = true;
              $('li.top').show();
    return;
  }

  $('ul').html(oldHtml);
  reversed = false;
      $('li.top').hide();
})
// call `.change()` on `.notice`
.change();

jsfiddle http://jsfiddle.net/bcbxftr6/82/

Post a Comment for "Apply The Checked Event When Loading In Jquery"