views:

461

answers:

4

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?

A: 

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>
RamboNo5
That will not work. When you instantiate a function as part of an expression, giving it a name will provide a way for the function to call itself recursively, but it will not set up that name as an attribute of the window object (or any other object).
Pointy
@RamboNo5 All `myfunction()` is triggering is the event binding of the `#search` element, not the click action itself.
Doug Neiner
Ok thanks! I edited thhe code. Now it should work as expected right?Your solution is much more slick, though :)
RamboNo5
@RamboNo5, basically, to follow your approach, you need to empty take the contents of the #search anonymous function, and put them into `myfunction()`, then use this to bind it to `#search` : `$("#search").click(myfunction);` And then whether you call `myfunction` or click the `#search` element, the same function will run.
Doug Neiner
Alright, that makes sense. A repeated binding of the click event is kinda superfluously.
RamboNo5
A: 
// first function
$(function() {
  $.yourFavoriteFunctionName = function() {
    // the code for the first function
  };
  $.yourFavoriteFunctionName();
});

then

// second function
$(function() {
  // whatever
  if (foo)
    $.yourFavoriteFunctionName();
Pointy
+4  A: 

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>
Juan
+1 for the simplest solution to the question asked. Great answer!
Doug Neiner
+2  A: 

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();
            }
        });
    });
harto
This is the correct way to go
PetersenDidIt
... except that you've polluted the global namespace instead of extending the jQuery object.
Pointy
@Pointy - If you start treating the jQuery object as a de facto global namespace, you'll run into the same problems as making all your variables global. What happens when we write the function `each()`?
harto
Besides, we aren't writing a plugin here. It makes no sense to modify the jQuery object. I also happen to think there's nothing inherently wrong with using the global namespace for user applications.
harto