You should indent blocks an additional level to make it more readable. That is,
Code:
function returnFriends() {
// Page that used window.open input to field
var inputField = ...
rather than
Code:
function returnFriends() {
// Page that used window.open input to field
var inputField = ...
Code:
function returnFriends() {
// Page that used window.open input to field
var inputField = window.opener.document.forms.friendsList.[COLOR="red"]elements.getElementById("name")[/COLOR],
The "
elements" property is an
HTMLCollection, not an
Element, and thus doesn't have a
getElementById method. If you want to use getElementById, call it on the form element (
window.opener.document.forms.friendsList.getElementById(...)), or you can access the input by the name attribute with
window.opener.document.forms.friendsList.elements.name (assuming the input is defined as
<input name="name" ... />).
I wish we could do this, but this can be problematic as it will loop over all direct (i.e. non-inherited) properties, not just integer indices.
This test will always succeed, as
i ranges from 0 through
check.length-1.
// objectsThatReturnCheckBoxesAsArray (please advise, assuming document.getElementsByTagName("inputs") but I do not know how to get only checked checkboxes)
You could get all inputs and check whether they are checkboxes before processing them:
Code:
var inputs = document.forms[0].elements,
values = [];
for (var i=0; i<inputs.length; ++i) {
if (inputs[i].type == 'checkbox') {
values.push(inputs[i].value);
}
}
window.opener.document.forms.friendsList.elements.names=values.join(', ');
Or write a function that filters a collection of HTMLElements:
Code:
Array.prototype.map = function(fn) {
var newSeq = [];
for (var i=0; i < this.length; ++i) {
newSeq[i] = fn(this[i]);
}
return newSeq;
}
Array.prototype.filter = function(keep) {
var newSeq = [];
for (var i=0; i < this.length; ++i) {
if (keep(this[i])) {
newSeq.push(this[i]);
} }
return newSeq;
}
function map(seq, fn) {
return Array.prototype.map.call(seq, fn);
}
function filter(seq, keep) {
return Array.prototype.filter.call(seq, keep);
}
...
// returns true for checked checkboxes and radio buttons
function isChecked(elt) {
return elt.checked;
}
function isCheckedCheckbox(elt) {
return (elt.type == 'checkbox') && elt.checked;
}
...
function returnFriends(fromForm, toInput) {
// if fromForm contains radio buttons, use isCheckedCheckbox
var checks = filter(isChecked, fromForm.elements);
toInput.value = checks.map(function(item){return item.value}).join(', ');
}
...
returnFriends(document.forms[0], window.opener.forms.friendList.elements.friendList);
Or use jQuery:
Code:
function returnFriends(fromForm, toInput) {
toInput.value=$.map(
window.opener.$(':checkbox:checked'),
function(item) { return item.value; }
).join(', ');
);
There are other alternatives, but they are mostly curiosities, so I won't mention them here.