Skip to main content
Inspiring
September 23, 2009
Question

Show/Hide onclick and onload

  • September 23, 2009
  • 2 replies
  • 4275 views

Hi,

I have a form with yes/no radio groups and a corresponding text box relating to the individual radio groups. Basically, if no is selected then the text box does not appear and if yes is selected the text box does appear. This part is working fine. What I need now is for editing purposes for when the page loads it will also show the text box and its contents. As it is now when I pull up any records at a later time the yes is selected but the text box is not visible unless I click no and then yes again. What I need is to have the text area show if it is applicable. The onclick event works but I dont know how to get the onload to work, if that would be the correct way of doing it. TIA.

Here is what I have at the moment.

<script type = "text/javascript">
<!--
function hide(x) {
document.getElementById(x).style.visibility="hidden";
}
function show(x) {
document.getElementById(x).style.visibility="visible";
}
//-->
</script>

<cfinput type="radio" id="sendbcastemail2" name="sendbcastemail" onClick="hide('showhidesendbcastemail')" value="No" checked="#sendbcastemail2_Checked#">No

<cfinput type="radio" id="sendbcastemail1" name="sendbcastemail" onClick="show('showhidesendbcastemail')" value="Yes" checked="#sendbcastemail1_Checked#">Yes

<cfdiv id="showhidesendbcastemail" style="visibility:#showhidesendbcastemaildate#">Select date:<br>

<cfinput type="datefield" name="sendbcastemaildate" value="#getLastedit.sendbcastemaildate#" validate="date" message="Enter a Valid Date" >

</cfdiv>

This topic has been closed for replies.

2 replies

Inspiring
September 29, 2009

Witihin the <script> tags after the functions you could add, assuming sendbcastemail1_Checked is either true or false.


<cfif sendbcastemail1_Checked EQ "True">

show('showhidesendbcastemail');

</cfif>


September 29, 2009

There is a possible problem (rare, but possible) with putting the code in the same script block is that it'll try to run the code as soon as that part of the page loads, and if it hasn't got the HTML part yet, it'll break as there's nothing to refer to.

If you move the <script> block below the HTML, then yes, it would work, but there are times when the HTML hasn't loaded yet, and the script shouldn't be run.

As an example, the office I'm currently in has a shared connection between 200+ people, and at times the download speed is less than 1kb/s, so imagine what that does to the page as it SLOWLY comes in, and tries to execute the code...

rmorganAuthor
Inspiring
September 29, 2009

No ideas?

September 29, 2009

Why can't you just call the hide() function in the onLoad part of the <body> tag?

<body onLoad="hide('showhidesendbcastemail')">

It might seem obvious, but if not, put some JavaScript at the bottom of the page to call the function...