Current location: Hot Scripts Forums » General Web Coding » JavaScript » Inserting HTMLInput Element value


Inserting HTMLInput Element value

Reply
  #1 (permalink)  
Old 09-11-08, 02:58 PM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
Inserting HTMLInput Element value

I have a textfield that uses a dynamic dropdown select script to let me choose a country by a few letters. Example, keying "B" shows "Brazil, Bulgaria, Belarus... Once i choose a county another function fires to send the selected data via http_request to another page. Here's it in use.

Code:
<input type="text" name="country" onkeyup="ajax_showOptions(this,'get_country',event)" onblur="makeRequest('insert.php?field_name=country&field_value=',this.value)">
The problem is that the *this.value* in makeRequest() only sends the "B" or whatever chars where entered in the field. Now I understand that the dropdown script is filling the field in a different way than I would have keyed it but how im not sure. Looking at FireBug i think i see that my selection "Brazil" shows under *HTMLInputElement*. I guess my question is how do i get that value to show instead of *this.value* in makeRequest().

FYI, each script works fine when not working together on the same field.

Thanks much for the help.
Reply With Quote
  #2 (permalink)  
Old 09-12-08, 01:46 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
can you post the code that actually sets the value of the field.
As far as I know, this.value always contains the data set in the field, even if it's set through javascript.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 09-12-08, 01:55 AM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
i have posted links to the code in my post. i am using 99% stock scripts.
Reply With Quote
  #4 (permalink)  
Old 09-12-08, 03:22 AM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
thanks to BrianOConnell -

Solution - replace *this.value* with *document.getElementById('country_hidden').value*.

The country_hidden element was not in my initial post because i didn't think it was relevant.. bad me. It is shown in the original example linked in my post.
Reply With Quote
  #5 (permalink)  
Old 09-12-08, 03:25 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Never mind.... misunderstood the solution
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #6 (permalink)  
Old 09-17-08, 04:58 AM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
darn.. found a glitch. the *document.getElementById('country_hidden').value* will not set on the first call of makeRequest(). It will though set if i re-select the drop down value again. Any ideas on hoe to get it to set the first time?
Reply With Quote
  #7 (permalink)  
Old 09-18-08, 10:04 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Your using the onblur event listener in your input element.
The problem with doing that is that the input element looses focus as soon as the div or iframe appears.
When your list appears there is no id returned until you select a value from the list.
So a null value is returned the first time.
Then if you again give the input element the focus and either enter another value or tab away from it, then the previous id will be returned, not the current value selected from the list.

To fix this problem you first need to remove the onblur event listener from the input element.
Then you add the makeRequest function call to the ajax_option_setValue function.

Something like this:
Javascript Code:
  1. function ajax_option_setValue(e,inputObj)
  2.     {
  3.         if(!inputObj)inputObj=this;
  4.         var tmpValue = inputObj.innerHTML;
  5.         if(ajax_list_MSIE)tmpValue = inputObj.innerText;else tmpValue = inputObj.textContent;
  6.         if(!tmpValue)tmpValue = inputObj.innerHTML;
  7.         ajax_list_activeInput.value = tmpValue;
  8.         if(document.getElementById(ajax_list_activeInput.name + '_hidden'))document.getElementById(ajax_list_activeInput.name + '_hidden').value = inputObj.id;
  9.         ajax_options_hide();
  10.         makeRequest('insert.php?field_name=country&field_value=',inputObj.id);
  11.     }

If you want to return the country name instead of the id then use tmpValue instead of inputObj.id

If I understand what you want to do correctly, then this should work.

One more thing,

From what I can tell is you aren't using the hidden input element, so remove it.
And shorten the ajax_option_setValue function to this:
Javascript Code:
  1. function ajax_option_setValue(e,inputObj)
  2.     {
  3.         if(!inputObj)inputObj=this;
  4.         var tmpValue = inputObj.innerHTML;
  5.         if(ajax_list_MSIE)tmpValue = inputObj.innerText;else tmpValue = inputObj.textContent;
  6.         ajax_list_activeInput.value = tmpValue;
  7.         ajax_options_hide();
  8.         makeRequest('insert.php?field_name=country&field_value=',inputObj.id);
  9.     }
__________________
Jerry Broughton

Last edited by job0107; 09-18-08 at 10:32 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] inserting data problematic reetha26 PHP 2 06-05-08 09:01 AM
Make A New Array From An Old Array (Excluding 1 Array Element) w2n PHP 14 08-17-07 03:24 PM
Two forms inserting into one table? moroose Database 3 08-06-07 04:51 PM
I most definately suggest DevelopingCentral.com For Any Website Design/Development! Salty777 General Advertisements 2 10-01-04 04:27 AM
getting an element by part of its name sliad JavaScript 1 05-26-04 08:03 AM


All times are GMT -5. The time now is 11:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.