943,808 Members | Top Members by Rank

Ad:
Apr 3rd, 2008
0

Function not returning value

Expand Post »
Hi all!

I have two functions, one which calls the other (here simplified) that are part of an autocomplete process. The scripts run without error, however the part I am posting here returns "undefined" for one of my variables. The alert in the function findValue() returns the correct "thisID" in the alert, however the function fillCodes() that called it returns "undefined" in its alert:


JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function findValue(li) {
  2. if( !!li.extra ) var thisID = li.extra[0];
  3. alert(thisID);
  4. return thisID;
  5. }
  6.  
  7. function fillCodes() {
  8. var oSuggest = $("#my_input_field")[0].autocompleter;
  9. var thisID = oSuggest.findValue();
  10. alert(thisID);
  11. }

Anybody have a clue?

Sandra
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SandraC is offline Offline
4 posts
since Apr 2008
Apr 3rd, 2008
0

Re: Function not returning value

You're never passing an argument to the findValue function. findValue requires an argument but it is never receiving one.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Apr 3rd, 2008
0

Re: Function not returning value

Click to Expand / Collapse  Quote originally posted by ShawnCplus ...
You're never passing an argument to the findValue function. findValue requires an argument but it is never receiving one.

findValue() is finding and alerting the correct ID - it just does not return a value.

I just learned that the findValue() function (jQuery plugin for suggest) is asynchronous and needs the event handler onFindValue() to be defined.

But I dont know how to do this


Sandra
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SandraC is offline Offline
4 posts
since Apr 2008
Apr 4th, 2008
0

Re: Function not returning value

Ok, clearly nobody knows how to define an event handler or use jQuery.

Since I don't know either, I have tried another approach.

First I created a hidden field with the name of "id_holder". I also have an input field where I want the returned value to be displayed named "id_target".

Then I changed my findValue() function to:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function findValue(li) {
  2. if( li == null ) return alert("No match!");
  3. if( !!li.extra ) var sValue = li.extra[0];
  4. document.my_form.id_holder.value = sValue;
  5. }

Now my calling function is:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function fillCodes() {
  2. var oSuggest = $("#my_input_field")[0].autocompleter;
  3. oSuggest.findValue();
  4. var this_holder = eval("document.my_form.id_holder");
  5. var this_target = eval("document.my_form.id_target")
  6. alert(this_holder.value);
  7. alert(this_holder.value);
  8. this_target.value = this_holder.value
  9. }

Now the curious thing is that the first alert is empty, while the second identical alert displays the correct id value. If the script does not contain at least one alert, then id_target remains empty.

Can anybody offer a solution as to how I can make this work without an alert? What is actually happening between the first and second alert?

If you are wondering why I don't put it all within the findValue() function, its because the final fillCodes() function will contain a loop to populate fields by sequence (the above code is simplified to make it easy to read).


Sandra
Last edited by SandraC; Apr 4th, 2008 at 1:01 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SandraC is offline Offline
4 posts
since Apr 2008
Apr 4th, 2008
0

Re: Function not returning value

Ok... I solved this by first firing findValue() when the user selects from the Suggest drop menu, then it is fired a second time when fillCodes() is fired (when the user chooses to add their selection to a list).


Sandra
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SandraC is offline Offline
4 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: image breaking
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: image verification





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


Follow us on Twitter


© 2011 DaniWeb® LLC