Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Can't get 'Read more/Read less' js code to work on my page

    Hi there,

    I'm building a website and on my 'About' page where there are some long paragraphs I would like to use the 'Read more/Read less' (or Expand/Collapse) jquery code. I'm not familiar with javascript at all so a laymen's explanation would be appreciated. What I've been able to do so far has been trial and error. However I can not, for the life of me, get this particular code to work on my page.

    This is what I'm trying to incorporate...

    ------> Javascript: ------>

    $('.more').click(function() {
    $(this).prev('div').slideToggle();
    $(this).html('close');
    });

    ------> HTML: ------>

    <div>
    This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!
    </div>

    <div style="display:none;">
    This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!
    </div>
    <a href="#" class="more">more</a>
    <br>
    <br>
    <br>
    <div>
    This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!
    </div>

    <div style="display:none;">
    This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!
    </div>
    <a href="#" class="more">more</a>

    It works fine on jsfiddle.net so I assume the problem is not with the code but with where I'm putting it on my page. Do I put the javascript inside a <script> or make it into a .js file? Does the <script> go into the page header or body? Does it have to be in the same <div> as the HTML it effects?

    This is a link to the page I want the code on...

    http://kimcoedesigns.com/about.html

    Any help/advice would be very appreciated.

  • #2
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <!-- don't forget jquery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    </head>
    
    <body>
    <div>
    This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!
    </div>
    
    <div style="display:none;">
    This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!
    </div>
    <a href="#" class="more">more</a>
    <br>
    <br>
    <br>
    <div>
    This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!
    </div>
    
    <div style="display:none;">
    This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!This is the paragraph to end all paragraphs. You
    should feel <em>lucky</em> to have seen such a paragraph in
    your life. Congratulations!
    </div>
    <a href="#" class="more">more</a>
    
    <script type="text/javascript">
    $('.more').click(function() {
    $(this).prev('div').slideToggle();
    ($(this).html() == "close") ? $(this).html("more") : $(this).html("close");
    });
    </script>
    </body>
    </html>
    Last edited by low tech; 07-14-2013 at 10:23 AM. Reason: improvement
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    Artymuse (07-16-2013)

  • #3
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thanks, low tech!

    Thank you so much, low tech.

    I was completely missing the jquery library ref in my code!

    After seeing your clear example I was able to see my problem and fix it with successful results. Thanks again!


  • #4
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    Hi Artymuse

    No problem, happy to help.

    Notice, I took the liberty of changing this line to 'close or more' otherwise original code would have just said close. Hope that is ok.

    Code:
    ($(this).html() == "close") ? $(this).html("more") : $(this).html("close");
    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.


  •  

    Tags for this Thread

    Posting Permissions

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