Forum Moderators: open

Message Too Old, No Replies

Javascript to toggle content

Cant find a simple code

         

newborn

1:17 am on Aug 21, 2007 (gmt 0)

10+ Year Member



Can you guys help me with a code that allows me to toggle a large amount of content. My home page has about 8-10 articles and i would like to achieve the follow:

Article header:
Small amount of content #*$!#*$! #*$!#*$! #*$!#*$! #*$!#*$!xx
More:

(Then when the more is clicked on it drops content and at the end of that content there is a line that says toggle article).

Help Guys

Receptional Andy

1:15 pm on Aug 21, 2007 (gmt 0)



Personally, I find that these sort of effects our easiest to achieve with a javascript library. Jquery [jquery.com] for instance, has built in functions to do exactly what you need.

Otherwise, you will need to access the content you want to toggle (e.g. using getElementById) and change the element's CSS to show and hide the content.

Trace

1:54 pm on Aug 21, 2007 (gmt 0)

10+ Year Member



How's this?
<html>
<head>
<title> My Page </title>
<script type="text/javascript">
function cacherVoir(theDIV){
leStyle = document.getElementById(theDIV).style ;
if(leStyle.display == "block") {
leStyle.display = "none";
}
else{
leStyle.display = "block";
}
}
</script>
</head>
<body>

Lorem ipsum dolor sit amet (<a href="#" onclick="cacherVoir('myDiv'); return false;">show/hide</a>)

<div id="myDiv" style="display:none;">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean eu sem sed turpis fringilla ornare. Duis risus sapien, ullamcorper non, pharetra eget, feugiat sit amet, dui. Vivamus dignissim. Phasellus aliquet eros ac velit laoreet elementum. Praesent vehicula lacus eget risus. Nunc auctor. Sed sit amet libero vel dui vestibulum venenatis. Nullam eget lorem. Aenean fermentum erat sit amet magna. Nam vehicula massa. Aenean eget mauris. Duis a pede. Suspendisse potenti. Curabitur condimentum eros eu nunc.</div>

</body>
</html>

newborn

7:17 pm on Aug 21, 2007 (gmt 0)

10+ Year Member



Thanks guys the script worked great now its off to setting up my website. Great.

Receptional Andy

7:32 pm on Aug 21, 2007 (gmt 0)



Not to be finicky, but I would suggest making the show/hide link javascript-only as it won't function otherwise. Similarly, hiding the div should be a javascript job. Graceful degradation and all that ;)

Trace

8:16 pm on Aug 21, 2007 (gmt 0)

10+ Year Member



Depending on how he wants it to appear on the site, he probably wants it hidden so it doesn't break the design. In which case for accessibility, I would create another page with the "Full Article" and link to it using the HREF.

People with JavaScript enabled browsers will get to read the DIV and others would be directed to another page containing the article.

Just another option.

Receptional Andy

8:39 pm on Aug 21, 2007 (gmt 0)



Not wanting to get into a javascript/accessibility debate, but it seemed from the original post that this is an existing page of content, that would benefit from enhanced functionality through javascript. I'm not sure it would make sense to make it so that javascript would be required for this to still work.

If the actual content for the 'more' links was located elsewhere, then maybe ajax would be a better bet?