...

View Full Version : Insert a value(s) to input from a checkbox



Wobbler
10-02-2003, 03:57 PM
I've made an adressbook there you can tick of a checkbox if you want to insert the email adress that the box has as a value to an <input Type=text> field. The adressbook opens in a popup.
So the script will insert the value from all the checked boxed (all named "to") and then close it self.

I've tried many ways but none has worked properly.

Any1 have an idea??

glenngv
10-02-2003, 04:25 PM
try:


<html>
<head>
<script type="text/javascript">
function addEmails(oForm){
var strEmails='';
if (oForm.to.length){
for (var i=0;i<oForm.to.length;i++){
if (oForm.to[ i ].checked){
if (strEmails!='') strEmails=','+oForm.to[ i ].value;
else strEmails=oForm.to[ i ].value;
}
}
}
else if (oForm.to.checked) strEmails=oForm.to.value;

if (opener && !opener.closed) {
if (strEmails!='') opener.document.parentForm.parentTo.value = strEmails;
else {
alert("Nothing to add.");
return;
}
}
else alert("Parent window does not exist. This popup will close.")

window.close();
}
</script>
</head>
<body>
<form>
<input type="checkbox" name="to" value="email1@blah.com">email1@blah.com<br>
<input type="checkbox" name="to" value="email2@blah.com">email2@blah.com<br>
<input type="checkbox" name="to" value="email3@blah.com">email3@blah.com<br>
<input type="checkbox" name="to" value="email4@blah.com">email4@blah.com<br>
<input type="button" value="Add" onclick="addEmails(this.form)">
<input type="button" value="Cancel" onclick="window.close()">
</form>
</body>
</html>


change parentForm to the name of the parent form and parentTo to the name of the field to insert email addrs to. I used comma as separator for the email addrs, change it accordingly.

Wobbler
10-02-2003, 04:40 PM
Yeak it works...

But, I've now got a problem, If I choose more then one email it will separate all the email with "," but only show the last email.

Like this " , , , email@adress.com"

And another question.
I've got one more checkbox (cc). How do i implant that one to the script. I've tried to clone the script but then it won't work full (but still some :D)

Thnx in before.

Wobbler
10-02-2003, 06:07 PM
any1?

glenngv
10-03-2003, 01:34 PM
Forgot the + :D



if (strEmails!='') strEmails+=','+oForm.to[ i ].value;
else strEmails=oForm.to[ i ].value;


Extending the script to add CC and BCC fields..



<html>
<head>
<script type="text/javascript">
function addEmails(oForm){
var strTO=getRecipients(oForm.to);
var strCC=getRecipients(oForm.cc);
var strBCC=getRecipients(oForm.bcc);

if (opener && !opener.closed) {
var parentF = opener.document.parentForm;
if (strTO!='') parentF.parentTO.value = strTO;
else if (strTo=='') {
alert("Please select TO recipients");
return;
}
if (strCC!='') parentF.parentCC.value = strCC;
if (strBCC!='') parentF.parentBCC.value = strBCC;
//also add checking for CC and BCC if they are required fields
}
else alert("Parent window does not exist. This popup will close.")

window.close();
}

function getRecipients(oRecip){
var strEmailAddrs='';
if (oRecip.length){
for (var i=0;i<oRecip.length;i++){
if (oRecip[ i ].checked){
if (strEmails!='') strEmailAddrs+=','+oRecip[ i ].value;
else strEmailAddrs=oRecip[ i ].value;
}
}
}
else if (oRecip.checked) strEmailAddrs=oRecip.value;
return strEmailAddrs;
}
</script>
</head>
<body>
<form>
<h4>TO:</h4>
<input type="checkbox" name="to" value="email1@blah.com">email1@blah.com<br>
<input type="checkbox" name="to" value="email2@blah.com">email2@blah.com<br>
<input type="checkbox" name="to" value="email3@blah.com">email3@blah.com<br>
<input type="checkbox" name="to" value="email4@blah.com">email4@blah.com<br>
<h4>CC:</h4>
<input type="checkbox" name="cc" value="email51@blah.com">email5@blah.com<br>
<input type="checkbox" name="cc" value="email6@blah.com">email6@blah.com<br>
<input type="checkbox" name="cc" value="email7@blah.com">email7@blah.com<br>
<input type="checkbox" name="cc" value="email8@blah.com">email8@blah.com<br>
<h4>BCC:</h4>
<input type="checkbox" name="bcc" value="email9@blah.com">email9@blah.com<br>
<input type="checkbox" name="bcc" value="email10@blah.com">email10@blah.com<br>
<input type="checkbox" name="bcc" value="email11@blah.com">email11@blah.com<br>
<input type="checkbox" name="bcc" value="email12@blah.com">email12@blah.com<br>
<p>
<input type="button" value="Add" onclick="addEmails(this.form)">
<input type="button" value="Cancel" onclick="window.close()">
</p>
</form>
</body>
</html>


change bold text to appropriate names in the parent window.

not tested... :D

Wobbler
10-04-2003, 07:13 PM
can you make the script disable all the other boxes if one of a type is checked so you can't check more than one adress. (e.g. 1 of To, 1 of CC and 1 of BCC)???

Wobbler
10-04-2003, 11:15 PM
I've tried a function that checks if a checkbox is checked, if it is, it will disable all with the same name. But that didn't work, I don't know if it was my code or something else.

glenngv
10-06-2003, 02:44 AM
huh? why won't you allow more than 1 email of each recipient type? Use radio button instead of checkbox then.



<html>
<head>
<script type="text/javascript">
function addEmails(oForm){
var strTO=getRecipient(oForm.to);
var strCC=getRecipient(oForm.cc);
var strBCC=getRecipient(oForm.bcc);

if (opener && !opener.closed) {
var parentF = opener.document.parentForm;
if (strTO!='') parentF.parentTO.value = strTO;
else if (strTo=='') {
alert("Please select TO recipients");
return;
}
if (strCC!='') parentF.parentCC.value = strCC;
if (strBCC!='') parentF.parentBCC.value = strBCC;
//also add checking for CC and BCC if they are required fields
}
else alert("Parent window does not exist. This popup will close.")

window.close();
}

function getRecipient(oRecip){
var strEmailAddr='';
if (oRecip.length){
for (var i=0;i<oRecip.length;i++){
if (oRecip[ i ].checked){
strEmailAddr=oRecip[ i ].value;
break;
}
}
}
else if (oRecip.checked) strEmailAddr=oRecip.value;
return strEmailAddr;
}
</script>
</head>
<body>
<form>
<h4>TO:</h4>
<input type="radio" name="to" value="email1@blah.com">email1@blah.com<br>
<input type="radio" name="to" value="email2@blah.com">email2@blah.com<br>
<input type="radio" name="to" value="email3@blah.com">email3@blah.com<br>
<input type="radio" name="to" value="email4@blah.com">email4@blah.com<br>
<h4>CC:</h4>
<input type="radio" name="cc" value="email51@blah.com">email5@blah.com<br>
<input type="radio" name="cc" value="email6@blah.com">email6@blah.com<br>
<input type="radio" name="cc" value="email7@blah.com">email7@blah.com<br>
<input type="radio" name="cc" value="email8@blah.com">email8@blah.com<br>
<h4>BCC:</h4>
<input type="radio" name="bcc" value="email9@blah.com">email9@blah.com<br>
<input type="radio" name="bcc" value="email10@blah.com">email10@blah.com<br>
<input type="radio" name="bcc" value="email11@blah.com">email11@blah.com<br>
<input type="radio" name="bcc" value="email12@blah.com">email12@blah.com<br>
<p>
<input type="button" value="Add" onclick="addEmails(this.form)">
<input type="button" value="Cancel" onclick="window.close()">
</p>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum