Jump to content

Click to reveal text


tmcdonnell

Recommended Posts

Hi all,

 

I would like a button/text link to open a text paragraph when it is clicked.

 

Example:

 

:D

 

Click to read

 

 

If they click it becomes:

 

 

:D

 

Thank you for clicking the icon.

 

 

To recap the text is hidden untill the user clicks a link or button.

 

How would I go about coding this?

 

Thanks in advance.

 

 

 

Link to comment
Share on other sites

Two ways, ajax or javascript. 

 

Javascript example:

 

<div id="swapdiv">
   Click to read.
</div>

<script type='text/javascript'>
var opened = false;

if(opened == false){
    document.getElementById('swapdiv').innerHTML = 'Thank you for clicking this icon.';
    opened = true;
}else{
    document.getElementById('swapdiv').innerHTML = 'Click to read';
    opened = false;
}
</script>

 

Something simple like that should work.

Link to comment
Share on other sites

Unless you are dealing with a LOT of text, AJAX would be overkill (and the delay would make it a poor choice as well). This is really a javascript/css issue, so this is in the wrong forum. But here is a down-and-dirty working example:

 

<script type="text/javascript">

function displayPara(fieldID, displayBol) {

  var paraObj = document.getElementById(fieldID);
  var showObj = document.getElementById(fieldID+'_show');
  var hideObj = document.getElementById(fieldID+'_hide');

  paraObj.style.display = (displayBol)? '' : 'none' ;
  showObj.style.display = (!displayBol)? '' : 'none' ;
  hideObj.style.display = (displayBol)? '' : 'none' ;

}

</script>

<a href="#" onclick="displayPara('para1', false);" id="para1_hide" style="display:none;">Hide Text</a>
<a href="#" onclick="displayPara('para1', true);" id="para1_show" style="">Show Text</a>
<div id="para1" style="display:none;">The text you want to hide/display will go here</div>

Link to comment
Share on other sites

In my opinion, size shouldn't be the limitation with AJAX; it should be a matter if the content is dynamic or not.

 

If you're looking to reveal changing, dynamic content, use AJAX.

 

If the text revealed is static and unchanging, use pure javascript.

 

Bryan

Link to comment
Share on other sites

In my opinion, size shouldn't be the limitation with AJAX; it should be a matter if the content is dynamic or not.

 

If you're looking to reveal changing, dynamic content, use AJAX.

 

If the text revealed is static and unchanging, use pure javascript.

 

Bryan

 

Well, I would partially agree. Although some people would consider "dynamically" displaying a paragraph as "dynamic" content, I do not. There is an important distinction there. True dynamic content (in my opinion) is content that you can't predetermine before the user clicks some particular action - such as paginated records or an IM tool.

 

With the user's request above I am assuming it is static text that will just be changing display property (if the text changed each time he expanded it then that would be dynamic). In those situation I typically support handling it all in the in-line JavaScript code.

 

However, no matter what your girlfriend says, size does matter. Even though many people have broadband connections - not all do. And even so, reducing bandwidth is somethign to pay attention to. If you have many different expandable sections with a lot of text, then it could be advantageous to dynamically load the content. There is no one rule fits all - each situation is different.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.