Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery call function by name

i have function

<script type="text/javascript">
    $(function () {
        $("#search").click(function() {
            var text = $("#searchText").val();
            $.getJSON("Search", { world: text, filter: text }, function(data) {
                $("tr.DataRow").toggle(false);
                for (i = 0; i < data.length; i++) {
                    $("#obj" + data[i]).toggle(true);
                }
            });
        })            
    });


</script>

now i have another function

<script type="text/javascript">

    $(function() {
        $('#searchText').bind('keypress', function(e) {
            if (e.keyCode == 13) {

            }
        });
    });
</script>

how can i call first function from second function?

like image 499
kusanagi Avatar asked Dec 15 '09 01:12

kusanagi


4 Answers

You can raise a click event on the element you registered the first function

<script type="text/javascript">

    $(function() {
        $('#searchText').bind('keypress', function(e) {
            if (e.keyCode == 13) {
                $('#search').click(); // Raise a click event on #search element
            }
        });
    });
</script>
like image 51
Juan Avatar answered Nov 09 '22 04:11

Juan


Extract the logic from the first event handler into a named function:

function doSearch() {
    var text = $("#searchText").val();
    $.getJSON("Search", { world: text, filter: text }, function(data) {
        $("tr.DataRow").toggle(false);
        for (i = 0; i < data.length; i++) {
            $("#obj" + data[i]).toggle(true);
        }
    });
}

You can now pass doSearch by name to the click handler:

    $(function () {
        $("#search").click(doSearch);
    });

and explicitly invoke it from within the key handler:

    $(function () {
        $('#searchText').bind('keypress', function(e) {
            if (e.keyCode == 13) {
                doSearch();
            }
        });
    });
like image 41
harto Avatar answered Nov 09 '22 04:11

harto


// first function
$(function() {
  $.yourFavoriteFunctionName = function() {
    // the code for the first function
  };
  $.yourFavoriteFunctionName();
});

then

// second function
$(function() {
  // whatever
  if (foo)
    $.yourFavoriteFunctionName();
like image 27
Pointy Avatar answered Nov 09 '22 05:11

Pointy


you could give it a name? am I missing something?

edit: to get this right

<script type="text/javascript">
function() myfunction{
    var text = $("#searchText").val();
    $.getJSON("Search", { world: text, filter: text }, function(data) {
        $("tr.DataRow").toggle(false);
        for (i = 0; i < data.length; i++) {
            $("#obj" + data[i]).toggle(true);
        }
    });
}

$(function(){
    $("#search").click(myfunction);
});
</script>

and then

<script type="text/javascript">

$(function() {
    $('#searchText').bind('keypress', function(e) {
        if (e.keyCode == 13) {
            myfunction();
        }
    });
});
</script>
like image 36
RamboNo5 Avatar answered Nov 09 '22 03:11

RamboNo5