views:

347

answers:

2

I have an if statement that needs to look like this: UPDATE

 $("input#textbox").keypress(function(e){
    key==e.which;
    if($("input#textbox").length <=7 && (key===13 || $("div#search-button").click())){
       /////SOME FUNCTION////
    };
 });

I'm trying to execute the "SOME FUNCTION" area only if the input length is <=7 and either the enter button is pressed or the "search" button is clicked.

Furthermore, I want to combine these 2 different function initiators so that they execute the same function but don't know how to do it:

  $("input#textbox").keypress(function(e){
     FUNCTION A
  };

  AND 

  $("div#search-button").click(function(){
     FUNCTION A
  };
+2  A: 

EDIT:

This is what you have to do:

  1. I am assuming that you want the text length and not number of textboxes.
  2. You want to execute FunctionA when enter is pressed on textbox or search button is clicked:

     $("input#textbox").keypress(function(e){
        key==e.which;
        if (key === 13) // if enter is pressed
        {
           if ("#textbox").val().length >= 7) //if textbox has more than 7 characters
           {
               functionA();
           }
        }
     });
     $("div#search-button").click(function(){ functionA();});

HTH

Raja
@Raja - please see UPDATE in Original Post
sadmicrowave
Please check out the solution now. HTH
Raja
A: 

This is how I would do it:

$("#search-button").click(function(){
    $("#textbox").keypress(function(e,clicked){
        (clicked || e.which===13) && $(this).val().length < 8 && functionA();
    }).trigger("keypress",[true]);
});
function functionA(){
    alert("hey!");
}
David Murdoch