943,958 Members | Top Members by Rank

Ad:
Apr 14th, 2008
0

Using a popup to fill an input field

Expand Post »
Hi,

I am looking for a way to use minimal javascript to open a popup window when the user clicks on an input field and allow them to choose one string inside the pop up window (like click on a radio button) and get the input box in the parent window filled with the value of the string.


I know it's kinda standard, but I dont know much about using javascript ... so I dont know exactly what to search for ....

I've seen hotmail allows you to populate your recipient input box using a popup ... I am looking to do something similar ...
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Apr 15th, 2008
0

Re: Using a popup to fill an input field

An oversimplified example would be:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. Parent.html
  2.  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  4. "http://www.w3.org/TR/html4/strict.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv"Script-Content-Type" content="text/javascript">
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <meta http-equiv="Expires" content="0"> <!-- disable caching -->
  10. <title>Parent</title>
  11. <script type="text/javascript">
  12. targetElement = null;
  13. function makeSelection(frm, id) {
  14. if(!frm || !id)
  15. return;
  16. targetElement = frm.elements[id];
  17. var handle = window.open('Child.html');
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <form id="frm" name="frm" action="#">
  23. <span>Name: </span><input name="txtName" id="txtName">
  24. <input type="button" value="Select Name" onclick="makeSelection(this.form, 'txtName');">
  25. </form>
  26. </body>
  27. </html>

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. Child.html
  2.  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  4. "http://www.w3.org/TR/html4/strict.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv"Script-Content-Type" content="text/javascript">
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <meta http-equiv="Expires" content="0"> <!-- disable caching -->
  10. <title>Example</title>
  11. <script type="text/javascript">
  12. function makeSelection(frm, id) {
  13. if(!frm || !id)
  14. return;
  15. var elem = frm.elements[id];
  16. if(!elem)
  17. return;
  18. var val = elem.options[elem.selectedIndex].value;
  19. opener.targetElement.value = val;
  20. this.close();
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <form id="frm" name="frm" action="#">
  26. <span>Names: </span>
  27. <select name="nameSelection">
  28. <option value="holly">Holly</option>
  29. <option value="golly">Golly</option>
  30. <option value="molly">Molly</option>
  31. </select>
  32. <input type="button" value="Select Name" onclick="makeSelection(this.form, 'nameSelection');">
  33. </form>
  34. </body>
  35. </html>

The code in itself is self explanatory but feel free to ask for explanations.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Apr 16th, 2008
0

Re: Using a popup to fill an input field

Yea, I can see how it works. Thank you.
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Mar 12th, 2010
0
Re: Using a popup to fill an input field
I have similar code to this; but my popup window shows a list of items pulled from a database table. each item is marked with 'a href'.

My problem is that only the numeric items work with the code. Nothing happens when I click on an item in the list that begins with a letter.

Here is the code that should transfer the selected item to the input text field on the form (html/coldfusion):

html Syntax (Toggle Plain Text)
  1. <a href="javascript:Pickvalue(#code#)">#code#</a>
  2.  
  3. function Pickvalue(picked) {
  4. <cfoutput>
  5. self.opener.document.yourform.#url.fieldname#.value = picked;
  6. </cfoutput>
  7. window.close();
  8. }

Why does this happen? Is it a javascript deficiency?
Last edited by Ezzaral; Mar 15th, 2010 at 8:14 pm. Reason: Added code tags. Please use them to format any code that you post.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kareem.ramos is offline Offline
1 posts
since Mar 2010

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: Date calculation
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: AJAX noob question





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


Follow us on Twitter


© 2011 DaniWeb® LLC