1

I have two jQuery functions on same element with same result. Is there a way to have a shorter code:

$(document).ready(function(){
    $('#task_<?php echo $var_t; ?>').dblclick(function(){
        if($('#taskd_<?php echo $var_t; ?>').is(':visible')){
            $('.tlistd').hide();
            $('.tlist').removeClass('t_ar');
        }else{
            $('.tlistd').hide();
            $('.tlist').removeClass('t_ar');
            $('#taskd_<?php echo $var_t; ?>').show();
            $('#task_<?php echo $var_t; ?>').addClass('t_ar');
        }
    })
    $('#td_arrow_down_<?php echo $var_t; ?>').click(function(){
        if($('#taskd_<?php echo $var_t; ?>').is(':visible')){
            $('.tlistd').hide();
            $('.tlist').removeClass('t_ar');
        }else{
            $('.tlistd').hide();
            $('.tlist').removeClass('t_ar');
            $('#taskd_<?php echo $var_t; ?>').show();
            $('#task_<?php echo $var_t; ?>').addClass('t_ar');
        }
    });
});

2 Answers 2

2

Yes it is possible. It is pretty simple you should create function and gave this function as input variable to event handler function. (i.e. .dbclick(), .click() and etc.). Since function also object in javasript.

$(document).ready(function(){
    $('#task_<?php echo $var_t; ?>').dblclick(do_action_name);
    $('#td_arrow_down_<?php echo $var_t; ?>').click(do_action_name);

    function do_action_name()
    {
        if($('#taskd_<?php echo $var_t; ?>').is(':visible'))
        {
            $('.tlistd').hide();
            $('.tlist').removeClass('t_ar');
        }
        else
        {
            $('.tlistd').hide();
            $('.tlist').removeClass('t_ar');
            $('#taskd_<?php echo $var_t; ?>').show();
            $('#task_<?php echo $var_t; ?>').addClass('t_ar');
        }
    }

});
Sign up to request clarification or add additional context in comments.

3 Comments

I must admit I saw something similar but I still don't know how to do it in this case.
The parentheses should be removed: .dblclick(do_action_name); and .click(do_action_name);, otherwise you pass the result of do_action_name(), in stead of the function itself, to the event handlers.
Yeap, that's it. My bad part was I created my function outside of document ready. Or who knows why... but it was not working. Now it is. Thank you! Voted up!
2

Create a function and bind it

$(document).ready(function(){
    function myFunction(){
        if($('#taskd_<?php echo $var_t; ?>').is(':visible')){
            $('.tlistd').hide();
            $('.tlist').removeClass('t_ar');
        }else{
            $('.tlistd').hide();
            $('.tlist').removeClass('t_ar');
            $('#taskd_<?php echo $var_t; ?>').show();
            $('#task_<?php echo $var_t; ?>').addClass('t_ar');
        }
    }
    $('#task_<?php echo $var_t; ?>').dblclick(myFunction);
    $('#td_arrow_down_<?php echo $var_t; ?>').click(myFunction);
});

1 Comment

there is a semicolon after the first call

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.