neat javascript?? vsnet2k3

G

Guest

hey all,

is there a neater way to write the following javascript code? Basically
there just 3 controls on my webform a button,checkbox,radio button. if you
check the checkbox it just enables the radiobutton. the button is just there
for postback testing.

<script type="text/javascript">
var rb
var cxb
function InzSr(){
rb=document.getElementById("RadioButton1")
cxb=document.getElementById("CheckBox1")
rb.disabled=(cxb.checked==true)?false:true
}

function test(cxb){
rb.disabled=(cxb.checked==true)?false:true
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<asp:CheckBox id="CheckBox1" runat="server"
onClick="test(this)"></asp:CheckBox>
<asp:RadioButton id="RadioButton1" runat="server"></asp:RadioButton></P>
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
<script type="text/javascript">InzSr();</script>
</form>
</body>
 
T

Tim_Mac

one simplification i can spot is as follows:

instead of:
rb.disabled=(cxb.checked==true)?false:true
just use:
rb.disabled = !cxb.checked;

the use of the conditional operator isn't necessary here, because the
expression inside the brackets evaluates to a boolean anyway.

also, just to be picky :) the "==true" is unnecessary because ".checked" is
already a boolean property.

hope this helps
tim
 
G

Guest

thank you very much.

Tim_Mac said:
one simplification i can spot is as follows:

instead of:
rb.disabled=(cxb.checked==true)?false:true
just use:
rb.disabled = !cxb.checked;

the use of the conditional operator isn't necessary here, because the
expression inside the brackets evaluates to a boolean anyway.

also, just to be picky :) the "==true" is unnecessary because ".checked" is
already a boolean property.

hope this helps
tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top