Results 1 to 3 of 3

Thread: Read more button

  1. #1
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Question Read more button

    Hi,

    I'm trying to create a read button on a website so that I can tidy up the web page. The text will be hidden by default and if a user clicks read more then they should get the rest of the text appear. I want multiple of these buttons to appear.

    I have got the core of the code, I think, but when I had more of the same button to on web page, no matter what button I press it will always show the more info of the first instance. Hope I'm making sense.

    this is my Java script and HTML code:

    Code:
    function ReverseDisplay() {
    if(document.getElementById('more').style.display == "none") { document.getElementById('more').style.display = "block"; }
    else { document.getElementById('more').style.display = "none"; }
    }

    HTML Code:
    <div id="morebutton" onclick='ReverseDisplay();'><a>Read More</a></div>
    <div id="more" style="display:none;">more info</div>
    The idea is that there will be multiple instances of the HTML code and the button that the user clicks will be the text related to that button.

    Thanks

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Yeh, that's not working because the script is activating the one id called "more", so if all of your hidden divs have matching ids, it's going to conflict.

    This key is to use a script that allows for different ids, which will help validate your markup too (you shouldn't use the same id multiple times on a web page)

    Try this script instead;
    Code:
    function ReverseDisplay(id) {
    var e = document.getElementById(id);
    e.style.display = ((e.style.display!='none') ? 'none' : 'block');
    }
    And your HTML markup should be;
    Code:
    <p>This is a short intro/excerpt for entry one. <a href="javascript:void(0);" onclick="ReverseDisplay('entry-01');this.innerHTML=(this.innerHTML=='Read More')?'Read Less':'Read More';">Read More</a></p>
    <div id="entry-01" style="display:none;">
    	<p>Entry 1 - blah, blah...</p>
    	<p>Entry 1 - blah, blah...</p>
    	<p>Entry 1 - blah, blah...</p>
    </div>
    
    <p>This is a short intro/excerpt for entry two. <a href="javascript:void(0);" onclick="ReverseDisplay('entry-02');this.innerHTML=(this.innerHTML=='Read More')?'Read Less':'Read More';">Read More</a></p>
    <div id="entry-02" style="display:none;">
    	<p>Entry 2 - ho-hum...</p>
    	<p>Entry 2 - ho-hum...</p>
    	<p>Entry 2 - ho-hum...</p>
    </div>
    
    <p>This is a short intro/excerpt for entry three. <a href="javascript:void(0);" onclick="ReverseDisplay('entry-03');this.innerHTML=(this.innerHTML=='Read More')?'Read Less':'Read More';">Read More</a></p>
    <div id="entry-03" style="display:none;">
    	<p>Entry 3 - do-by-do...</p>
    	<p>Entry 3 - do-by-do...</p>
    	<p>Entry 3 - do-by-do...</p>
    </div>
    Note the hidden div ids that need to match the corresponding "ReverseDisplay" ids in the "Read More" links.

    Hope that helps
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    gemzilla (07-18-2013)

  4. #3
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    Thank you that was very useful

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2013, 10:52 PM
  2. Place Play Button outside image and reflect button status...
    By elmstreet in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 02-02-2010, 10:14 PM
  3. You have got to read this !...
    By pcbrainbuster in forum The lounge
    Replies: 39
    Last Post: 04-09-2007, 07:48 PM
  4. My IE does not read the CSS?
    By slava in forum CSS
    Replies: 4
    Last Post: 02-08-2007, 11:23 PM
  5. Read This
    By ausant in forum The lounge
    Replies: 8
    Last Post: 11-11-2004, 06:54 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •