0

i have following code which is working fine on click button but i also want it to execute on change event of my drop down list. which is

 $("select#search_status").change() //i want same execution for it do i need to duplicate whole code ?


$('.search-btn-cls').click(function () {
    var search_text = $('#search_text').val();

    search_val = true;
    initiatePagination();
    // ajaxSortPaginationSearch();
    // show_per_page();
    // getpagination();

    if (search_text.length > 2 || search_text.length < 1) {
        ajaxSortPaginationSearch();
        show_per_page();
        getpagination();
    } else {
        alert('Please enter minimum 3 serach character.');
        $('#search_text').focus();
    }

});
1
  • somebody posted this answer $('.search-btn-cls').on("click change",function () { and now he removed. is that not the right solution? Commented Jun 30, 2017 at 14:10

2 Answers 2

4

Instead of using anonymous function for event handling, create a function and call it in both cases i.e on click and on change.

// Create a function.
function handleDropdown() {
    var search_text = $('#search_text').val();

    search_val = true;
    initiatePagination();
    // ajaxSortPaginationSearch();
    // show_per_page();
    // getpagination();

    if (search_text.length > 2 || search_text.length < 1) {
        ajaxSortPaginationSearch();
        show_per_page();
        getpagination();
    } else {
        alert('Please enter minimum 3 serach character.');
        $('#search_text').focus();
    }

}
// Now call the function.
$('.search-btn-cls').click(handleDropdown);
$("select#search_status").change(handleDropdown);
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice answer.
0

Try this

//Call both events with same function that you want to execute.
$('.search-btn-cls').click(main);
$('.search-btn-cls').change(main);

        function main(){
            var search_text = $('#search_text').val();

            search_val = true;
            initiatePagination();

            if (search_text.length > 2 || search_text.length < 1) {
                ajaxSortPaginationSearch();
                show_per_page();
                getpagination();
            } else {
                alert('Please enter minimum 3 serach character.');
                $('#search_text').focus();
            }
        }

Comments

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.