473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Java Script help

. .
Hi

I need some help with java script . I have a ASPX page that is pulling a
user message heading from the table on the page . The user message
header has message body which suppose to dispay under the heading when a
user click on the message header . Message body will come from database
table .The different message header has different message body . I have
got the message heading showing up on the web page . How do I show the
message body show under the header . Here is an example of what I like
to do

Message header : Break time ( this is a click )

Message Body : Please take the break on time and come back to work on
time
Please help .

*** Sent via Developersdex http://www.developersdex.com ***
Sep 13 '05 #1
2 2111
.. . schrieb:
Hi

I need some help with java script . I have a ASPX page that is pulling a
user message heading from the table on the page . The user message
header has message body which suppose to dispay under the heading when a
user click on the message header . Message body will come from database
table .The different message header has different message body . I have
got the message heading showing up on the web page . How do I show the
message body show under the header . Here is an example of what I like
to do

Message header : Break time ( this is a click )

Message Body : Please take the break on time and come back to work on
time


One possible way is, packing all the bodies in not displayed div-containers, ie
like the following:

<h1><a href="#" onclick="showBody(1);">Break time</a></h1>
<div id="body1" class="bodytext">Please take the break on time and come back to
work on time</div>

<h1><a href="#" onclick="showBody(2);">Break time</a></h1>
<div id="body2" class="bodytext">Please take the break on time and come back to
work on time</div>

and so on

Next thing: Define your CSS for hiding the message-bodies:

..bodytext {
display: none;
}

And now you need the function for displaying the message body:

function showBody(bodyNumber) {
var bodyID = "body_" + bodyNumber;
document.getElementById(bodyID).style.display = block;
}

If the user clicks the Header, the body will be shown.

And to make it a little bit nicer for people without JS, you can set the
display: none by JS, so people withou JS will allways see the message bodies.
Simply change the class to "display: block;" and on load of the document, you
apply something like this:

var elements = document.getElementsByTagName("div");
for(var i=0; i<elements.length; i++) {
if (elements[i].className == "bodytextvisible") {
elements[i].style.display = "none";
}
}

All this is untestet but should bring you up to a possible solution :)

greetings,

martin
Sep 13 '05 #2
Martin Kurz wrote:
. . schrieb:
Hi

I need some help with java script . I have a ASPX page that is pulling a
user message heading from the table on the page . The user message
header has message body which suppose to dispay under the heading when a
user click on the message header . Message body will come from database
table .The different message header has different message body . I have
got the message heading showing up on the web page . How do I show the
message body show under the header . Here is an example of what I like
to do

Message header : Break time ( this is a click )

Message Body : Please take the break on time and come back to work on
time

One possible way is, packing all the bodies in not displayed div-containers, ie
like the following:


Having things hidden by default then exposed by script means that anyone
with scripting disabled or not available has no way of finding the
hidden content.

<h1><a href="#" onclick="showBody(1);">Break time</a></h1>
Using an A element solely to create a clickable element is not a good
idea. Much better to put the onclick directly on the element and use
CSS to provide the UI hints that it can be clicked on.

If an A element is used, then using href="#" will cause most browsers to
scroll to the top of the page when the element is clicked - very
annoying if the user has scrolled the page down at all.

The href should link to a page the does something useful (e.g. replicate
the functionality of the onclick) and the onclick should return false to
prevent following the link if scripting is enabled (it will also stop
any scrolling caused by #).

Some would also argue that using an H1 element purely for its style
attributes is contrary to good design, use CSS instead.
<div id="body1" class="bodytext">Please take the break on time and come back to
work on time</div>

<h1><a href="#" onclick="showBody(2);">Break time</a></h1>
<div id="body2" class="bodytext">Please take the break on time and come back to
work on time</div>

and so on

Next thing: Define your CSS for hiding the message-bodies:

.bodytext {
display: none;
}

And now you need the function for displaying the message body:

function showBody(bodyNumber) {
var bodyID = "body_" + bodyNumber;
document.getElementById(bodyID).style.display = block;
The display value 'block' must be quoted, and it is much better to
toggle between '' and 'none' using a single function.
}

If the user clicks the Header, the body will be shown.
Only if scripting is enabled, otherwise they will just see the page
scroll to the top and wonder what is going on.

And to make it a little bit nicer for people without JS, you can set the
display: none by JS, so people withou JS will allways see the message bodies.
Simply change the class to "display: block;" and on load of the document, you
apply something like this:
There is no need to put the class into a CSS rule, it is just for
convenience. If a single showHide() function is created that toggles
between '' and 'none', then the intialisation function just has to call
that and the divs will be hidden.

The divs may have more than one class, so it is always good to test the
class name as a whole string - but after testing that the div has a
class and that the style object is supported.

Finally, the scope of getElementsByTagName can be constrained by putting
all the divs of interest inside another element (say a div) so not every
single div in the document is looped over.

A basic design problem is how to identify the content to be hidden when
the header is clicked on. An ID can be used as here, another way is to
use the document structure and know that the element to hide is say the
first div after the one that was clicked on - but that requires that the
document adhere to the particular structure chosen and any variances may
create difficult to find errors.

var elements = document.getElementsByTagName("div");
for(var i=0; i<elements.length; i++) {
if (elements[i].className == "bodytextvisible") {
elements[i].style.display = "none";
}
}

[...]

Tested in IE and Firefox:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Show/hide content</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<style type="text/css">
..headDiv {
font-weight: bold;
text-decoration: underline;
font-size: 150%;
}
</style>
<script type="text/javascript">

function showHide(el)
{
el.style.display = ('none' == el.style.display)? '' : 'none';
}

function toggleEl( el )
{
var id = 'cont-' + el.id.split('-')[1];
showHide(document.getElementById(id));
}

function initDivs( id )
{
if (document.getElementById && document.getElementsByTagName) {
var divs = document.getElementById(id).getElementsByTagName(' div');
if ( divs && divs[0].style ) {
var d;
var i = divs.length;
var re = /\bheadDiv\b/;
while ( i-- ) {
d = divs[i];
if ( re.test(d.className) ){
toggleEl( d );
d.onclick = function() {toggleEl(this);};
d.style.cursor = 'pointer';
}
}
}
}
}
</script>

</head>
<body onload="initDivs('messageHolder');">

<div id="messageHolder">
<div class="headDiv" id="head-1">Here is a header 1</div>
<div class="contDiv" id="cont-1">Here is the content 1</div>
<div class="headDiv" id="head-2">Here is a header 2</div>
<div class="contDiv" id="cont-2">Here is the content 2</div>
<div class="headDiv" id="head-3">Here is a header 3</div>
<div class="contDiv" id="cont-3">Here is the content 3</div>
</div>

</body>
</html>

--
Rob
Sep 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andy R. | last post by:
Hello everyone, I've spent quite some time now, looking for some information on how to get this done, sadly none has helped me much, though. I have a bit of java scrpt on a webpage (.php) to...
0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
3
by: StealthMonkey | last post by:
Let me prefix this by saying that I know next to nothing about Java (so please try to keep explainations simple). I use PHP for my server-side web programming. Here is my dilemma: I need a...
4
by: Laura P | last post by:
Hi, I wasn't sure whether this should be posted in the Java are or in a Solaris thread, so I shall post it in both. Sorry for the duplication. I am new to Solaris and am having trouble...
4
by: Don Grover | last post by:
I hope some one can help, I have a html table that is created with asp that has a row of repeated buttons down the side. these call a page passing a query string with invoice number. I need to...
4
by: Jeff | last post by:
Hi, Are there anyone can help me? My questions are as following: Because I have a tools bar which coding with javascript, it will reuse very often. So I want to separate the client-side java...
9
by: Stephen H. | last post by:
Hi, I have an existing web application with java beans that I wanne migrate to ASP.NET using C#. The existing web application has some jsp files that use java beans as follows: .... <%@...
33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
9
by: Mickey Segal | last post by:
The long-simmering Eolas patent dispute: http://www.microsoft.com/presspass/press/2003/oct03/10-06EOLASPR.mspx has led to an optional Microsoft Update: http://support.microsoft.com/kb/912945/en-us...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.