how to implement keydown and focusout in single jsp in j query?

623 views Asked by At

i am looking to implement key down and focusout method in single jsp.. the following requirement i have to meet..

Requirements:

  1. (keyDown) > when the user entering greater than "four chatacter"(That is product name) in the text box i have open on pop up window and have to show related products name what i have entered in text box.

  2. (focusout) > when the user entering exact product name(it may be 7 character or 8 character) and make a focusout and i have to get the exact product name..

So i implemented this two things and see my code below..

        $("productName").keydown(function()
        {

           //when productName.length >4 i have show the pop up and show the related productName in that code..
          //passing greater than 4 character to ajax and and retriving related products from ajax response.. and showing in the pop up

        }

       $("productName").focusout(fuction()
        {
           //when the user entering exact product name(it may be 7 character or 8 character) and make a focusout and have the pass the value to ajax and have retrieve exact productName details from the ajax response
        }

issues faced

  • when i try to enter exact product name(may be 6 or 7 character) the key down is being called because it crossed greater than four character..

So how can i over come these things.. this two requirements is feasible to meet at a time?

Please help me out from this issue

1

There are 1 answers

2
Indranil Mondal On

Use keyup instead of keydown. Just write a if check in the keydown function, if the product name is matched, the rest of the function will not be executed, thus it'll only execute the focus function.

var productname = "Indranil";
$("#productName").keyup(function (e) {

if ($(e.currentTarget).val().length >= 4 && $(e.currentTarget).val() != productname) {
    console.log("keydown called");
}
})
$("#productName").focusout(function (e) {
if ($(e.currentTarget).val() == productname) {
    console.log("focus out called");
}
})