Search code examples
phposclass

In OSClass, Call Jquery when change the categories


I need to call Jquery/JavaScript function when change the categories in OSCLASS.

I used following codes,

<div class="row">
    <label for="catId"><?php _e('Category', 'modern'); ?> *</label>
    <?php ItemForm::category_select(null, null, __('Select a category', 'modern')); ?>
</div>

How i can Call?


Solution

  • Function ItemForm::category_select() generate a select html element with class and id catId.

    <select name="catId" id="catId"> 
    ....
    </select>
    

    You can add jquery code like this:

    $("#catId").change(function(){
        var cat_id = $(this).val();
        if(cat_id != '') {
            alert('Category Id : ' + cat_id );
        }
    });
    

    You can add this code directly to the theme page, or via hook wrapping the previous code into a function.

    <?php 
    function _add_javascript() { 
        if(Params::getParam('page')=='items' && 
    (Params::getParam('action')=='post' || Params::getParam('action')=='item_edit') ) {    
    ?>
    <script>
        $("#catId").change(function(){
            var cat_id = $(this).val();
            if(cat_id != '') {
                alert('Category Id : ' + cat_id );
            }
        });
    </script>
    <?php
        }
    }
    ?>
    
    <?php osc_add_hook('footer', '_add_javascript'); ?>