Click to See Complete Forum and Search --> : Checkbox & Select
nik_crash
03-18-2003, 06:58 PM
Sir/Ma'am,
Is there a way in javascript, everytime i clicked a checkbox & select one of the option in select will automatically compute the value.How can I do that?
ex.
<select>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<input type="text" name="ex1" readonly> //here the output will goes here
<input type="checkbox" value="101" id="chk1" name="chk1" >101
<input type="checkbox" value="102" id="chk2" name="chk2">102
Thank you in advance..
Nedals
03-18-2003, 07:56 PM
<select onChange="someJSfunction()">
Activates whenever you change the selection.
and/or
<input type="checkbox" value="101" id="chk1" name="chk1" onClick="someJSfunction()">
Acitvates when you 'check' OR 'uncheck' the box
jce_step
03-18-2003, 08:09 PM
here is an example :
<script>
var s, chk1, chk2;
function init(){
s=document.getElementById("s");
ex1=document.getElementById("ex1");
chk1=document.getElementById("chk1");
chk2=document.getElementById("chk2");
}
function sel(){
ex1.value= s.value;
}
</script>
<body onload="init();">
<select id="s" onChange='sel()'>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<input type="text" id="ex1" readonly> //here the output will goes here
<input type="checkbox" value="101" id="chk1">101
<input type="checkbox" value="102" id="chk2">102
</body>
I hope it could be of any help.
nik_crash
03-18-2003, 10:02 PM
Sir/Ma'am,
Thank u for answering my question but can you give me simple function that computes. In your example whn i clicked the checkbox no return value appear in textbox?
Thanks..
jce_step
03-20-2003, 06:29 PM
<script>
var s, chk1, chk2, ex1;
function init(){
s=document.getElementById("s");
ex1=document.getElementById("ex1");
chk1=document.getElementById("chk1");
chk2=document.getElementById("chk2");
}
function sel(){
ex1.value= s.value;
}
function one(){
if(chk1.checked != false)
ex1.value = 101;
}
</script>
<body onload="init();">
<select id="s" onChange='sel()'>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<input type="text" id="ex1" readonly> //here the output will goes here
<input type="checkbox" id="chk1" onclick='one()'/>101
<input type="checkbox" value="102" id="chk2"/>102
</body>
-----------------------------------------------------------------
Ok try whith the code above, it shoud work for checkboxes too!
Nedals
03-20-2003, 09:43 PM
1. What is it you are trying to compute?
2. Generally, although someone may dispute this, you should use 'name' rather than 'id' for your form elements.
3. I recommend that you put your elements within <form> tags. Although, I'm told, it will work with IE, it definately will NOT work in NS.
4. You really don't need that 'init()' function. It's better to call out the elements (in full) within the sel() and one() functions. If someone were to resize the screen, you will sometimes loose the setups performed by init() thus requiring a reload. This is definately true for NS.