16

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?

1
  • 13
    @kusanagi It is considered proper courtesy to mark the correct answers to your questions as "accepted". You have asked 11 questions, so it will be very easy to go back through them and accept all the correct answers. This will encourage people to answer your questions fully and without reservation. Commented Dec 15, 2009 at 1:39

4 Answers 4

20

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>
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the simplest solution to the question asked. Great answer!
20

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();
            }
        });
    });

4 Comments

... except that you've polluted the global namespace instead of extending the jQuery object.
@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()?
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.
Plus this is only hitting the global namespace if it's declared outside any other functions. For basic situations if this code only needs to be re-used within a single page (eg if an number of events need to call the same custom animation on a page) then it will probably already be within a closure inside the $(document).ready(function(){.....}) If it needs to be reused more widely then turn it into a plugin and give it it's own file.
12
// first function
$(function() {
  $.yourFavoriteFunctionName = function() {
    // the code for the first function
  };
  $.yourFavoriteFunctionName();
});

then

// second function
$(function() {
  // whatever
  if (foo)
    $.yourFavoriteFunctionName();

Comments

1

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>

5 Comments

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).
@RamboNo5 All myfunction() is triggering is the event binding of the #search element, not the click action itself.
Ok thanks! I edited thhe code. Now it should work as expected right? Your solution is much more slick, though :)
@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.
Alright, that makes sense. A repeated binding of the click event is kinda superfluously.

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.