PDA

View Full Version : script help



jackavin
09-10-2007, 04:04 AM
i need to do check box that if i check it will add value to another textfield
and i have about 3 check box and id check which one it will add value only not replace
how can i do that thx

jscheuer1
09-10-2007, 04:38 AM
Please restate your question more clearly, and supply example markup.

Twey
09-10-2007, 04:43 AM
<script type="text/javascript">
function map(f, a) {
for(var i = 0, r = []; i < a.length; r.push(f(a, i++)));
return r;
}

function filter(f, a) {
for(var i = 0, r = []; i < a.length; f(a[i++], i) ? r.push(a[i]) : '');
return r;
}

function catVals(els) {
return map(function(el) { return el.value; }, filter(function(el) { return el.checked; }, els)).join("\r\n");
}
</script>
</head>
<body>
<!-- ... -->
<form action="">
<textarea name="vals">
</textarea>
<input type="checkbox" value="cats" onclick="this.form.elements.vals.value = catVals(this.form.elements.chks);" name="chks">
<input type="checkbox" value="dogs" onclick="this.form.elements.vals.value = catVals(this.form.elements.chks);" name="chks">
</form>

jscheuer1
09-10-2007, 04:53 AM
I haven't looked over Twey's above response, which is probably good. I was just playing around with the idea as best that I understood it. I came up with this:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function cbox(box, tf){
if(box.checked)
box.form.elements[tf].value=isNaN(box.form.elements[tf].value-0)? box.value : (box.form.elements[tf].value-0)+(box.value-0);
else
box.form.elements[tf].value=box.form.elements[tf].defaultValue;
}
</script>
</head>
<body>
<form action="#">
<div>
<input type="text" name="num" readonly value="1">
<input type="checkbox" name="bob" value="10" onclick="cbox(this, 'num');">
</div>
</form>
</body>
</html>

as a possible avenue toward what you are asking about.

Twey
09-10-2007, 04:58 AM
I read "add" as meaning "concatenate." Clarify please, jackavin?

jackavin
09-10-2007, 05:43 AM
Thank for all post but i want something like hotmail that add contact to CC field when u click on the name it will add more and more and when uncheck that name will out of the textfield something like that

thank you

Twey
09-10-2007, 05:55 AM
Yes, that's what I thought you meant. See my code.

jscheuer1
09-10-2007, 06:10 AM
Well, that certainly sounds like concatenate, as Twey (mind reader that he is :) ) already offered. Does his code work for you or not?

In any case, have a look at this:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function cbox(box, tf){
var dat=box.checked? ' '+box.value : new RegExp(' '+box.value);
if(box.checked)
box.form.elements[tf].value+=dat;
else
box.form.elements[tf].value=box.form.elements[tf].value.replace(dat, '');
}
</script>
</head>
<body>
<form action="#">
<div>
<input type="text" name="addys" readonly><br>
<input type="checkbox" name="bob" value="someone@some.com" onclick="cbox(this, 'addys');"> someone@some.com
</div>
</form>
</body>
</html>

jackavin
09-10-2007, 06:22 AM
thank you all without u i will die :D

jscheuer1
09-10-2007, 06:45 AM
Here's more (from my take on this):


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function cbox(box, tf){
var dat=box.checked? ';'+box.value : new RegExp(';\?'+box.value);
if(box.checked)
box.form.elements[tf].value+=dat;
else
box.form.elements[tf].value=box.form.elements[tf].value.replace(dat, '');
box.form.elements[tf].value=box.form.elements[tf].value.replace(/^;/, '');
}
</script>
</head>
<body>
<form action="#">
<div>
<input type="text" name="addys" readonly size="75"><br>
<input type="checkbox" name="someone0" value="someone0@some.com" onclick="cbox(this, 'addys');"> someone0@some.com<br>
<input type="checkbox" name="someone1" value="someone1@some.com" onclick="cbox(this, 'addys');"> someone1@some.com<br>
<input type="checkbox" name="someone2" value="someone2@some.com" onclick="cbox(this, 'addys');"> someone2@some.com<br>
</div>
</form>
</body>
</html>

Added Later:

But, unless this isn't all that critical, none of this will be all that good if there isn't a server side fall back.

jackavin
09-11-2007, 07:39 AM
jscheuer1
thank for update code i have another thing how can i make it check i try put check on radiobuuton but the text not display on textfield

thankyou

jscheuer1
09-11-2007, 03:36 PM
Radio buttons are specifically designed to be deployed in groups where only one of the group of radio buttons may be selected. They do not lend themselves to use with this setup, particularly since you appear to want to let the user select multiple items, a single item, or none.

jackavin
09-12-2007, 12:41 AM
so sorry i mean checkbox can i make it default thx :p

jscheuer1
09-12-2007, 03:18 AM
You can supply a default check box and a default value for the text field in the normal manner:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function cbox(box, tf){
var dat=box.checked? ';'+box.value : new RegExp(';\?'+box.value);
if(box.checked)
box.form.elements[tf].value+=dat;
else
box.form.elements[tf].value=box.form.elements[tf].value.replace(dat, '');
box.form.elements[tf].value=box.form.elements[tf].value.replace(/^;/, '');
}
</script>
</head>
<body>
<form action="#">
<div>
<input type="text" name="addys" value="someone0@some.com" readonly size="75"><br>
<input type="checkbox" checked name="someone0" value="someone0@some.com" onclick="cbox(this, 'addys');"> someone0@some.com<br>
<input type="checkbox" name="someone1" value="someone1@some.com" onclick="cbox(this, 'addys');"> someone1@some.com<br>
<input type="checkbox" name="someone2" value="someone2@some.com" onclick="cbox(this, 'addys');"> someone2@some.com<br>
</div>
</form>
</body>
</html>

jackavin
09-12-2007, 04:27 AM
tahnk you for ur kind