BliarOut Posted September 16, 2010 Share Posted September 16, 2010 I know I'm just a full stop, a bracket or an apostrophe away from the answer to this but I can't see it. I have the following code which checks that a user has selected a radio button then opens a pop up window and passes a few variables to it. It works perfectly as pasted below: //============================================================+ //Used to open 'popup' windows to display print documents. //Takes the parameters url, name of the submitting form, the //radio button name, the width, the height and the discipline ID //============================================================+ function popup(url, frmName, ctrlName, hw, hh, typeID) { //============================================================+ //Check if the radio button has been selected and get it's value. //============================================================+ clickedOn=''; for (var i=0; i < document.PTWSelect.UniqueID.length; i++) { if (document.PTWSelect.UniqueID[i].checked) { clickedOn='document.PTWSelect.UniqueID[i].value'; var uniqueID = document.PTWSelect.UniqueID[i].value; } } if (clickedOn == ""){ alert("Please make a selection.") return false; } //============================================================+ //Set some variables for the popup. //============================================================+ var width = hw; var height = hh; var left = (screen.width - width)/2; var top = (screen.height - height)/2; var params = 'width='+width+', height='+height; params += ', top='+top+', left='+left; params += ', directories=no'; params += ', location=no'; params += ', menubar=no'; params += ', resizable=yes'; params += ', scrollbars=yes'; params += ', status=no'; params += ', toolbar=no'; newwin=window.open(url+'?TypeID='+typeID+'&UniqueID='+uniqueID,'formWindow', params); if (window.focus) {newwin.focus()} return false; } //============================================================+ //End function. //============================================================+ The problem is if I try to use a passed variable in this line it fails: if (document.PTWSelect.UniqueID[i].checked) What is the correct syntax, I thought this would work? if (document.frmName.ctrlName.UniqueID[i].checked) But it doesn't. If I test it by doing an alert(frmName +" "+ctrlName) the variables come through just as I expected. What am I missing? Thanks, Rob Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 16, 2010 Share Posted September 16, 2010 And what error(s) are you getting? I think I know what the problem is, but not positive from your explanation. "frmName" & "ctrlName" are variable names created from values passed into the function. You cannot use them like this: if (document.frmName.ctrlName.UniqueID[i].checked) When that code is encountered the javascript parser is looking for a form with the literal name "formName". It does not evaluate the value of the variable "formName". But. the solution is simple. Just use the object collection reference and use the variable accordingly. Give this a try: if (document.forms[frmName].elements[ctrlName].UniqueID[i].checked) Although, I'm not sure that would work. What is UniqueID supposed to reference? It might help if you show the HTML form or at least the relevant part. Quote Link to comment Share on other sites More sharing options...
BliarOut Posted September 22, 2010 Author Share Posted September 22, 2010 Well it did work despite my explanation :-) Thanks very much for your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.