An HTML Form has 3 textboxes & each of the 3 textboxes has a corresponding checkbox (meaning 3 checkboxes). I want that when a user checks the 1st checkbox, the 1st textbox should get disabled. When he unchecks the 1st checkbox, the 1st textbox should be made editable. Same with the other 2 textboxes & checkboxes. In order to disable/enable the 3 textboxes, I framed the following code:
The above code is working fine. But the same thing can be done in the following way also:
As shown in the above 2 codes, in the 1st code, the 3 checkboxes invoke 3 different JavaScript functions whereas in the 2nd code snippet, only one JavaScript function is invoked by the 3 checkboxes. What I would like to know is which of the above 2 methods is better & why? Or is it because both the codes are client-side scripts, it doesn't make any difference whether the 1st route is adopted or the 2nd one?
Thanks,
Arpan
Code:
<script language="JavaScript">
function dt1(){
if(document.myForm.chk1.checked==true){
document.myForm.txt1.disabled=true
}
else{
document.myForm.txt1.disabled=false
}
}
function dt2(){
if(document.myForm.chk2.checked==true){
document.myForm.txt2.disabled=true
}
else{
document.myForm.txt2.disabled=false
}
}
function dt3(){
if(document.myForm.chk3.checked==true){
document.myForm.txt3.disabled=true
}
else{
document.myForm.txt3.disabled=false
}
}
</script>
<form name="myForm" action="SomePage.html">
<input type=text name="txt1">
<input type=checkbox name="chk1" onClick="dt1()">
<input type=text name="txt2">
<input type=checkbox name="chk2" onClick="dt2()">
<input type=text name="txt3">
<input type=checkbox name="chk3" onClick="dt3()">
</form>
The above code is working fine. But the same thing can be done in the following way also:
Code:
<script language="JavaScript">
function dt(){
if(document.myForm.chk1.checked==true){
document.myForm.txt1.disabled=true
}
else{
document.myForm.txt1.disabled=false
}
if(document.myForm.chk2.checked==true){
document.myForm.txt2.disabled=true
}
else{
document.myForm.txt2.disabled=false
}
if(document.myForm.chk3.checked==true){
document.myForm.txt3.disabled=true
}
else{
document.myForm.txt3.disabled=false
}
}
</script>
<form name="myForm" action="SomePage.html">
<input type=text name="txt1">
<input type=checkbox name="chk1" onClick="dt()">
<input type=text name="txt2">
<input type=checkbox name="chk2" onClick="dt()">
<input type=text name="txt3">
<input type=checkbox name="chk3" onClick="dt()">
</form>
As shown in the above 2 codes, in the 1st code, the 3 checkboxes invoke 3 different JavaScript functions whereas in the 2nd code snippet, only one JavaScript function is invoked by the 3 checkboxes. What I would like to know is which of the above 2 methods is better & why? Or is it because both the codes are client-side scripts, it doesn't make any difference whether the 1st route is adopted or the 2nd one?
Thanks,
Arpan