Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Hiding and showing divs using javascript

Posted on 2008-10-09
2
Medium Priority
?
299 Views
Last Modified: 2012-05-05
The attached code snippet gives my attempt to hide and show divs using javascript.  Right now I am not getting any response from any of the radio button clicks and just need some help understanding why.



THE JAVASCRIPT FUNCTION:
 
function show_div(div_id) {
    // hide all the divs
    document.getElementById('url').style.display = 'none';
    document.getElementById('content').style.display = 'none';
 
    // show the requested div
    document.getElementById(div_id).style.display = 'block';
}
 
 
 
THE CALL TO THE JAVASCRIPT FUNCTION
 
		<tr>
			<td align="right">Content Type:
			</td>
			<td>
				Remote: <input type="radio" name="CreativeContentType" value="Remote" <cfif #CreativeContent.CreativeContentType# eq 'Remote'>checked</cfif> onclick="show_div('url'); return false;">
				Hosted: <input type="radio" name="CreativeContentType" value="Hosted" <cfif #CreativeContent.CreativeContentType# eq 'Hosted'>checked</cfif> onclick="show_div('content'); return false;">
			</td>
		</tr>
 
 
 
 
 
THE DEFINED DIVS
 
		<div style="display: none;" id="url">
			<tr>
				<td nowrap align="right" valign="top">URL: 
				
				</td>
				<td nowrap>
					<INPUT TYPE="text" SIZE=80 NAME="RemoteContentUrl" ID="RemoteContentUrl" class="Forminput" value="#CreativeContent.RemoteContentUrl#">&nbsp;
					<input name="Content" type="hidden" value="">
				</td>
			</tr>
		</div>
		<div style="display: none;" id="content">
			<tr>
				<td align="right">Content:
				</td>
	
				<td><input name="RemoteContentUrl" type="hidden" value="">
					<textarea name="Content" id="Content" cols="100" rows="12" wrap="soft" 
						class="OptsFormData">#CreativeContent.Content#</textarea>
				</td>
			</tr> 
		</div>

Open in new window

0
Comment
Question by:onaled777
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1000 total points
ID: 22678616
get rid of the div tags and instead move the:
style="display: none;" id="url" to the tr element. A table may NOT have div around tr, so the browser is ignoring that markup. Ultimately your markup should look similar to:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
//THE JAVASCRIPT FUNCTION: 
function show_div(div_id) {
    // hide all the divs
    document.getElementById('url').style.display = 'none';
    document.getElementById('content').style.display = 'none';
 
    // show the requested div
    document.getElementById(div_id).style.display = 'block';
}
 
 </script>
 
<!-- THE CALL TO THE JAVASCRIPT FUNCTION -->
 <table>
                <tr>
                        <td align="right">Content Type:
                        </td>
                        <td>
                                Remote: <input type="radio" name="CreativeContentType" value="Remote" onclick="show_div('url'); ">
                                Hosted: <input type="radio" name="CreativeContentType" value="Hosted" onclick="show_div('content'); ">
                        </td>
                </tr>
  
 
 
 
<!-- THE DEFINED DIVS -->
  
                        <tr style="display: none;" id="url">
                                <td nowrap align="right" valign="top">URL: 
                                
                                </td>
                                <td nowrap>
                                        <INPUT TYPE="text" SIZE=80 NAME="RemoteContentUrl" ID="RemoteContentUrl" class="Forminput" value="#CreativeContent.RemoteContentUrl#"> 
                                        <input name="Content" type="hidden" value="">
                                </td>
                        </tr> 

                        <tr style="display: none;" id="content">
                                <td align="right">Content:
                                </td>
        
                                <td><input name="RemoteContentUrl" type="hidden" value="">
                                        <textarea name="Content" id="Content" cols="100" rows="12" wrap="soft" 
                                                class="OptsFormData">#CreativeContent.Content#</textarea>
                                </td>
                        </tr>
 </table>
</body>
</html>

Open in new window

0
 

Author Comment

by:onaled777
ID: 22679164
This worked perfectly thank you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question