Newbie to Javascript

W

w.bristow

Hi all,

I have found a script on the web which lets me click on an image and it
gives focus & fills in a certain radio button corresponding to id. It
works, however I want to know how it works, for reference.

IN HEADER

<script language="JavaScript" type="text/JavaScript">
function setCheckedValue(radioObj, newValue) {
if(!radioObj)
return;
var radioLength = radioObj.length;
if(radioLength == undefined) {
radioObj.checked = (radioObj.value == newValue.toString());
return;
}
for(var i = 0; i < radioLength; i++) {
radioObj.checked = false;
if(radioObj.value == newValue.toString()) {
radioObj.checked = true;
}
}
}
</script>

IN BODY

<td colspan="2" rowspan="2" valign="top" bgcolor="#CCFFCC"
class="boxhomiss"
onMouseOver="MM_swapImage('rentcard','','/Schemes/Images/rentcards/rentcaxton.gif',1)"
onMouseOut="MM_swapImgRestore()"><img src="/Schemes/Images/gho.gif"
alt="HEAD OFFICE" width="51" height="57"
onclick="setCheckedValue(document.forms['monopoly'].elements['Base'],
'Caxton');"></td>
<td rowspan="2" valign="middle" bgcolor="#CCFFCC"
class="boxhomissbut"
onMouseOver="MM_swapImage('rentcard','','/Schemes/Images/rentcards/rentcaxton.gif',1)"
onMouseOut="MM_swapImgRestore()">
<input type="radio" name="Base" id="Caxton"
value="Caxton"></td>

Hope some one can explain the script to me, I understand the onclick
part, as this is just calling the function with the element 'Base' (the
radio button group) and radio button 'Caxton'. I know this much as I
have managed to edit the script to work for me the bit I don't
understand all that much is The Header section bit.
 
L

lucanos

I'll try and translate it to you, and it should make sense in context
then.

<script language="JavaScript" type="text/JavaScript">

function setCheckedValue(radioObj, newValue) {
// Requires two values.
// radioObj = the document object refering to the Radio Button.
// newValue = the value that button should hold when selected.
if(!radioObj)
// If the provided radioObj does not exist, then exit
the Script
return;
var radioLength = radioObj.length;
// Set the variable radioLength to the number of instances of
the radioObj object
if(radioLength == undefined) {
// If there is only 1 instance of radioObj
radioObj.checked = (radioObj.value ==
newValue.toString());
// If the value of that radio button = newValue, the
mark it as selected/checked
return;
// Exit the Script
}
for(var i = 0; i < radioLength; i++) {
// For each of the instances of the radioObj object
radioObj.checked = false;
// Set it to unselected/unchecked
if(radioObj.value == newValue.toString()) {
// If the value of that instance of the radio
button = newValue, the mark it as selected/checked
radioObj.checked = true;
}
}
}

</script>

Hope that helps...

Luke
 

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

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top