Nov 8, 2009

Hiding/showing Text On Click - is it possible with css/javascript

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Designing > Web Design and HTML

Hiding/showing Text On Click - is it possible with css/javascript

warbird
Ok, I got an question: I want to make a site with a news box or something similar. It's gonna be a table with a small piece of text and an "read more" button. Now I want to make it so that if people click on the "read more" there will appear an other piece of text underneath it. How do I do this?

thanks in advance,
-=jeroen=-

Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
Here's a simple JavaScript code that will show/hide a div when clicked. Simply link this with a button instead of the text in the SPAN tag. This is the code used for the SPOILER BBCode. This can achieve the effect you're seeking with just minor adjustments.

CODE

<script type='text/javascript'>

mytagid = Math.floor( Math.random() * 100 );

document.write("<div style='margin: 3px;padding:4px;background:#fff;border:1px dotted #386898'>
<div id='"+mytagid+"_closed' style='display:none'>
....
This is the text I want to hide/show
........
</div>

<br />

<a href='#' onclick='toggleview("+'"'+mytagid+'_closed'+'"'+");

return false;'>

<span style='font-size:smaller'>Read More...</span></a></div>");

document.close();

</script>

 

 

 


Comment/Reply (w/o sign-up)

ruben
Hi,

I believe m^e's code won't work, since he forgot to include the script for the toggleview() function code. It is also a bit too complicated for what you want. Oh and all the document.write things have to be in one line, otherwise you'll get an "unterminated string literal" error.

Let's do this extensively ^^
To hide an HTML element, you just have to include the attribute style with no-display.
CODE
<div style="display:none">

This means that the <div> is not there at all. If you want to make it invisible, but keep the space as placeholder, then you would use "visibility: hidden" instead of "display: none".
Since some browsers do not support Javascript/users turned it off, the field should be shown by default. If you don't care about those without JS, then hide the div by default.
CODE

<div><script type="text/javascript"><!--
document.write("<div style='display:block' id='moreinfo'>");
//-->
</script>More Info, shown for those without Javascript<script type="text/javascript"><!--
document.write("</div>");
//--></script></div>

This way, the div will be shown to the users which have JS disabled, but it will be directly hidden from the ones with document.write-supporting browsers (nearly all). It's a lot of action for those few users without Javascript, but you should not neglect them. If you intend to do so, then do it directly.
CODE

<div style="display:none" id="moreinfo">More Info, shown for those without Javascript</div>

like this.

But from now on, the code for the JS-browsers
Now, let's show this div :-)
you need only one simple JS-command for this purpose..
CODE

document.getElementById('moreinfo').style.display = 'none';

You can call this function with the onclick-eventhandler on any element, that you can click (almost all)
CODE

<a href="#" onclick="document.getElementById('moreinfo').style.display = 'block';">Show me what you got</a>

So, in the end your document would look something like this...
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
 <title>Untitled Page</title>
</head>

<body>
<p><a href="#" onclick="document.getElementById('moreinfo').style.display = 'block';">Show me what you got</a></p>
 <div><script type="text/javascript"><!--
document.write("<div style='display:none' id='moreinfo'>");
//-->
</script>More Info, shown for those without Javascript<script type="text/javascript"><!--
document.write("</div>");
//--></script></div>
</body>

</html>


works a 100%, I tried it out.
Good luck, feel free to ask back!

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : hiding, showing, text, click, css, javascript

  1. Help With Making A Textbased Game
    im new to text based games (2)
  2. Animated Favicon With Scrolling Text!
    Free Animated FavIcon With Scrolling Text! (13)
    Anybody needs Animated FavIcon with your site address scrolling on that along with your logo?
    Check this site! Thats the site without any customization Options! But if you need to customize
    the Animated Icon, please reply here (In the sense, Fonts & Color Customization) Im doing it
    for Free! Example is here: checkOut the fav icon for: www.niranvv.com and
    www.fun.niranvv.com You can see one Icon along with one Scrooling Text: NiranVv.Com in the
    icon! So, if you need that one, ask here! I'll give you the animated favIcon(Gif Format) ....
  3. Css For Input Text Only
    I don't think there is a solution but... (17)
    I have read (or glimse through) all the CSS 2 specification and google a lot but found no solution
    for the following problem. Anyone can help? The basic idea is the input field for the type text ,
    password is quite small, so I want to change its width by CSS CODE input {   width :
    400px; } However, this affects the width of the radio button too (in most current browser). Is
    there anyway that I can apply this CSS to input field which is of the type text or password
    ONLY? The tricky part is I MUST NOT use any additional attribute in input field, ....
  4. Xhtml Is Not Suppose To Be Text/html
    IE not planning to support XHTML yet (8)
    I am quite disappointed in IE's development as with keeping up but still if they feel they need
    to fully work on current things that they have already attempted to resolve and perfect then that is
    a good thing, but development should have never got so messy and only the developer would know that
    it's his fault and not MS's. This isn't to bad mouth IE, but it's so sad to know
    that even after 5 years or more of IE6, IE7 has no intention of supporting XHTML and I don't
    think I can wait for IE8 if development takes such a long time. They should also....
  5. Html: Alt Text For Objects
    Is it possible? (8)
    My site is going to use a lot of Flash files pluged into the page. I embed it into the page like
    this: CODE What I want to know is: Is it possible to give the object an alt text?
    Something like the alt="The Flash File" that is used in IMG tags is what I want. I've tried
    putting it inside the object tag, param tag and embed tag. That didn't work.....
  6. Wrap
    I am trying to wrap text after a textbox (2)
    I am trying to wrap text after a textbox. It is working fine on IE. but it doesn't work on
    Netscape 7.0 and Firefox 1.0.1 /huh.gif" style="vertical-align:middle" emoid=":huh:" border="0"
    alt="huh.gif" /> Following is the code: CODE Airport/city
    name or 3 letter code. The "Airport/city name or 3 letter code. " text waps
    around in IE by not in NN or firefox. Do somebody tell me how to do it? Notice
    from m^e: Please put your code between CODE tags from next time onwards. That ....
  7. Help(right Click)
    (10)
    I know that similar issues have been discussed in this forum, and that someone with enough knowledge
    can get around the "right-click, View Source" issue. But I'd like to know the code to stopping
    the right click on my images, as hubby is trying to halt some of the stealing of his pictures
    online. If you could please help me with that code, I'd be really grateful.....
  8. Game Website
    text base (4)
    I want to make a online game website, can I do with astohost? I been trying to get a free website
    but it's seems to harder then I thought, I hope this one work, everytime I apply they never send
    a confirmation email. Will the site I get from astohost be able to do what I want, a text base game
    with some animation, like flash, I need help so if anyone can help let me know.....
  9. Good Text Html Editor
    (24)
    Does someone know good text HTML editor for web page creation?(sure, I mean text editor, not
    WYSWYG). There is no reason to stick with Notepad when there is a set of the various special html
    editors much more convenient than ms Notepad. No any of microsoft products does not not suit for
    html editing, and Frontpage is absolute rotten.....
  10. wut is html?
    hyper text (4)
    wut is html? well html is a kind of languge i think and it is used to make website? i think thats
    html is one of thy best website making languges....
  11. Protect images from Right-click
    No scripts - just a small trick (5)
    Offen happens to see your images used in someone's website. Here is a little trick how protect
    them, from the right-click 'Save Image As..' If you use tables, you can use the
    'real' picture as backgroung in a cell and use a 1x1 tranparent GIF to cover it. See the
    code to understand. CODE ....
  12. Text-Based HTML Editors
    (5)
    1st Page 2000 1st Page 2000 is the tool that lets you create powerful, great looking websites
    fast, easier and on-time. AceExpert In addition to being an excellent HTML editor, AceExpert
    also greatly assists you with predefined Java applets and JavaScripts. AceExpert is the solution for
    developers who want to integrate the latest technologies into their Web pages. Simple and easy to
    use, AceExpert gives you the full power of a complete HTML Editor. Arachnophilia Arachnophilia
    lets you use two methods to create Web pages: The 'old-fashioned way' with th....

    1. Looking for hiding, showing, text, click, css, javascript

See Also,

*SIMILAR VIDEOS*
Searching Video's for hiding, showing, text, click, css, javascript
advertisement



Hiding/showing Text On Click - is it possible with css/javascript

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com