943,169 Members | Top Members by Rank

Ad:
Aug 30th, 2010
0

jQuery function executing, but not at the right time

Expand Post »
I've been having trouble with a seemingly simple function...I have a form where 3 of 5 field values are generated by a script tied in with Google Search (like a business address). The user is prompted to enter their business license #, and as soon as the input field has val().length == 9 characters, I want to execute an ajax call among some other functions...

The problem is the function works when there are 9 characters in the field, but only after I generate a new and different set of values for the generated fields (i.e. the first generated values, coupled with those 9 characters isn't enough to set off the function, although when i use a second set of generated values, the function kicks in).

Here's the applicable code...although there's a lot more to the picture...

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // when the business licence text field is focused (should be 9 characters long)
  2. $('#ubi').focus(
  3. function() {
  4. var ubiVal = $('#ubi').val();
  5.  
  6. if(ubiVal.length == 9) {
  7.  
  8. //Set some values I'll need for my ajax call...
  9. var bnameVar = $("#title").val();
  10. var ubiVar = $("#ubi").val();
  11.  
  12. //hide Google search results w/ form-value generator buttons...
  13. $('#searchResults').toggle(false);
  14.  
  15. //This is the div I want to show once #ubi is 9 characters...
  16. $('#verified').toggle(true);
  17. //Loading...
  18. $('#verified').append("<div id='ajaxBusy'><img src='images/addmapp_loading.gif' class='loadingText' /><h3 class='loadingText'>Searching business records...</h3></div>");
  19.  
  20. //Loading display...
  21. $('#ajaxBusy').css({
  22. display:"none",
  23. margin:"3px",
  24. paddingTop:"0px",
  25. paddingBottom:"0px",
  26. position:"absolute",
  27. width:"auto"
  28. });
  29.  
  30. //Loading text display...
  31. $('#loadingText').css({
  32. display:"inline",
  33. position:"absolute",
  34. top:"7px",
  35. left:"40px",
  36. });
  37.  
  38. //Use 9-digit number from #ubi to perform my ajax function & handle results...
  39. $.getJSON("addmapp_sandbox/tim/scraper/readdol.php?ubi=" +
  40. ubiVar + "&name=" +
  41. bnameVar + "",
  42. function(data){
  43. var ubi = data.ubi
  44. ubi = ubi.substring(0,9);
  45. $("#verified").html("<h3 class='loadingText'>" +
  46. data.firmName + "</h3><h4>" +
  47. ubi + " </h4><h4>" +
  48. data.isMatch + "</h4><h4>"+
  49. data.locationAddress+"</h4>"
  50. );
  51. });
  52. } // End if
  53. } // End function()
  54. ); //End ('#ubi').focus()

It should work like: User types a business name into a text input, google search results asynchronously begin to appear as the user types, user finds said business within a set of results, clicks 'this is me', certain google results are appended to the form, THEN *** the user inputs a ubi number and the moment they type the 9th digit the rest of the function takes place...***

I would really appreciate the help, if even just a prod in the right direction. Thanks in advance!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
seniramsu is offline Offline
6 posts
since May 2010
Aug 30th, 2010
0
Re: jQuery function executing, but not at the right time
Ha, nevermind, I have a strong suspicion I'm just outta my mind.

I answered my own question.

The functions won't work properly because the field is already in focus when i type the 9 digits in, so I have to blur (click out of) the field and then refocus (click back into) the field for the if statement to be iterated over again and the code to realize that now there are 9 digits within the field upon focus.

Whew.

There is hope yet.

Cheers! And thanks for considering to help me out..if you did.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
seniramsu is offline Offline
6 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: jQuery if statement trouble
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript Query Parser





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC