Search code examples
javascriptjqueryhtmlicheck

how to scroll to a div after checked input radio


I use the icheck jquery plugin for style the input radio

I need to when checked the radio scroll to the special div id

<input type="radio" class="checkb" name="selectplan" value="P6302" id="P6302">

And script

<script>

    $(document).ready(function () {
        $('input').iCheck({
            checkboxClass: 'icheckbox_square-red',
            radioClass: 'iradio_square-red',
            increaseArea: '20%' // optional
        });
    });
</script>

Solution

  • You could listen to the "isChecked" event of the icheck library and scroll to the div with the id of the checkbox name:

    $('input').on('ifChecked', function(event) {
    
       // Fixed scroll target       
       const target = $('#target');
    
       // For dynamic targets: adjust the target selector like
       // const target = $($(event.target)).attr('name');
    
       $('html,body').animate({
          scrollTop: target.offset().top
       }, 1000);
    });
    

    In case you want the scroll to happen whenever the checkbox is clicked, use the ifToggled event instead.

    Soures: https://github.com/fronteed/icheck#callbacks, jQuery Scroll to Div, https://api.jquery.com/event.target/, http://api.jquery.com/attr/