Set form field from popup window in JavaScript?
I have a page that pops up a window via JavaScript. On that page, I want to list out several options as hyperlinks. When the user clicks a link, I want the popup window to close and to select an item in a combobox based on what the user clicked. So for example, say I have the following items in a combobox on the main screen:
1
2
3
4
5
On the popup window, I have the following links:
1
2
3
4
5
When the user clicks say 2, the window closes, and item 2 gets selected in the combobbox. Any help or examples would be appreciated.
Re: Set form field from popup window in JavaScript?
Quote:
Originally posted by dbassettt74
Any help or examples would be appreciated.
Here you go, i have put together this example for you.
page1.htm
Code:
<html>
<head>
<title>Example</title>
<script>
function SelectUrl()
{
var winW=475;
var winH=345;
var w=screen.width;
var h=screen.height;
var t=h/2-winH/2;
var l=w/2-winW/2;
var url = "list.htm";
var hWnd = window.open(url,"","top=" + t + ", left=" + l + ", width=" + winW+ ",height=" + winH+ ",resizable=yes,status=yes,scrollbars=yes");
if ((document.window != null) && (!hWnd.opener))
hWnd.opener = document.window;
}
</script>
</head>
<body>
<form name=form1>
Name :
<select name=list1 size=1>
<option value='danial'>Danial Atique</option>
<option value='robert'>Robert Steven</option>
<option value='joe'>Joe Blogs</option>
</select>
<input type=button onclick="JavaScript:SelectUrl()" value="Select">
</form>
</body>
</html>
save this page as list.htm
Code:
<html>
<head>
<title>Example</title>
<script>
function SelectIt(id)
{
var d;
d=window.opener.document.form1;
for (i=0;i<d.list1.options.length;i++)
{
var l;
l=d.list1.options[i].value;
if (l.indexOf(id)==0)
{
d.list1.options[i].selected=true;
self.close();
}
}
}
</script>
</head>
<body>
1. <a href="JavaScript:SelectIt('danial')">Danial Atique</a> <br>
2. <a href="JavaScript:SelectIt('robert')">Robert Stiven</a> <br>
3. <a href="JavaScript:SelectIt('joe')">Joe Blogs</a> <br>
</body>
</html>