1

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
  };
5
  • What you're referring to is applicable to all of javascript. Adding parenthesis likely isn't your problem. You should paste the code that is breaking. Also, that semi-colon after your closing bracket could cause errors. Commented Apr 21, 2010 at 16:58
  • That's valid syntax, though I think you mean expression, not statement. Not sure what the issue is. More info may help. Commented Apr 21, 2010 at 16:58
  • @all - see the update in my OP Commented Apr 21, 2010 at 17:07
  • Unless jquery is doing something very odd, it seems like $("div#search-button").click() isn't going to return something that will tell you it was clicked... Commented Apr 21, 2010 at 17:28
  • @Tetsujin - true, so whats a remedy for that? Commented Apr 21, 2010 at 17:29

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

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!");
}

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.