• Resolved scottcarlton

    (@scottcarlton)


    I have been trying to create a show/hide div in the wp loop without any success. This simple script work great for one, but in the loop it toggles everything or nothing. Looking at this I need to be able to append something to each one that gets created so that it only works for that one only. Such as this on there bios. Not even sure if this is the right script to work with. Any help out there?

    <script type=”text/javascript”>
    <!–
    function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == ‘block’)
    e.style.display = ‘none’;
    else
    e.style.display = ‘block’;
    }
    //–>
    </script>

Viewing 15 replies - 1 through 15 (of 30 total)
  • <-- these are for comments in HTML -->

    Remove the comments if you want this to run. Also see the forum rules for posting code.

    @swansonphotos:

    Wrapping JavaScript in an HTML comment used to be standard practice to keep browsers that didn’t support JavaScript from having problems. The JavaScript inside will still run regardless. However, it is true that since practically every browser used today can understand JavaScript, there’s no real reason to do it anymore.

    @scottcarlton:

    The problem is that your script is probably targeting your entire content section. The best way to toggle specific content is by using jQuery. If you’re trying to show/hide individual posts, you can give a link to your site and I’ll try to come up with a working code example; it shouldn’t be too hard. If you’re looking to hide content within a post/page you can also check out this plugin:

    https://www.ads-software.com/extend/plugins/jquery-collapse-o-matic/

    @ Big Bagel,
    In this form?

    That’s how you would do it if you were using XHTML, right above it is the example for HTML like the OP’s code example:

    https://en.wikibooks.org/wiki/JavaScript/Placing_the_Code#Inline_HTML_comment_markers

    Isn’t the default DOCTYPE for WordPress sites XHTML 1.0 Transitional?

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    And what about stripping HTML tags?

    @swansonphotos:

    The DOCTYPE is completely up to the theme (look in header.php). Plus, using the wrong example won’t break anything other than validation. With the browsers used today it’s best just to not use either.

    And what about stripping HTML tags?

    What?

    https://www.w3.org/TR/xhtml1/#h-4.8

    strip_tags is used by several WordPress functions including the excerpt.

    Commenting JavaScript in this fashion was required a very long time ago, but unless you are running Netscape 1 or IE4, it’s not required and can cause validation issues are even be stripped out confusing WP.

    Additionally, the JavaScript should be in a separate file, the file called in the head or footer removing any such issues.

    I also disagree about validation issues, if a page is not able to be validated based on the DOCTYPE called, it can and will cause issues. I will continue to rely on the expertise of the those who continue to strive towards validation.

    @swansonphotos:

    I’m aware of strip_tags. I’m assuming the OP’s code is being included directly in the theme (in header.php for example). It wouldn’t be run through any functions so strip_tags isn’t an issue.

    Additionally, the JavaScript should be in a separate file, the file called in the head or footer removing any such issues.

    I know, but we weren’t taking about that. The OP’s code was obviously being included directly in the file, so I constrained my recommendations to such. My offer to give a jQuery example, if taken, would have included this fact.

    I also disagree about validation issues, if a page is not able to be validated based on the DOCTYPE called, it can and will cause issues.

    I never said validation issues should be ignored. I simply said that the only issue with accidentally using the wrong technique in this very specific case would be that it won’t validate. In other words, it will still work as expected in all relevant browsers. Further, since we can’t check the DOCTYPE of the OP’s theme anymore, this doesn’t really matter.

    Commenting JavaScript in this fashion was required a very long time ago, but unless you are running Netscape 1 or IE4, it’s not required and can cause validation issues are even be stripped out confusing WP.

    I know…that’s what I’ve been saying. From my previous posts:

    However, it is true that since practically every browser used today can understand JavaScript, there’s no real reason to do it anymore.

    With the browsers used today it’s best just to not use either.

    Since these posts don’t have much of anything to do with the OP’s original topic it would be best to continue asking questions in a new topic or, if you want to direct them specifically towards me, on my site.

    Thread Starter scottcarlton

    (@scottcarlton)

    @big Bagel
    Thanks for your response. Ya, the code as of now is in the header.php file. Not sure if that is best, but as a newbie and not an experienced js coder or wordpress coder, it’s the easiest for me. This issue is that it does target all hidden divs causing them all to open or close. The other issue that I came across in the loop is that it will target only one div instead of the div in that particular loop posting. I keep search google for a fix, but haven’t found one yet.
    As for the plugin, it looks great, but I am trying to stay away from plugins as much as possible. I would rather learn how to do it versus just adding a plugin for something. Helps me learn. Just a personal thing. Plus my trying to make it user friendly as possible for my client. So shortcode just adds one more think to teach my client.

    @swansonphotos
    Thanks for the posting code rules. I’m new to the forums and didn’t realize the code posting. It will be helpful in the future

    If providing a link to the site is possible, that would be super helpful. Without actually seeing the structure of the site and exactly what you want hidden/shown it’s hard to suggest anything specific.

    Thread Starter scottcarlton

    (@scottcarlton)

    Here is a link to what I am trying to accomplish so what.

    So I have my get_title and professional title with image in a box with the “a href” attribute and get_content and contact info in a hidden div until clicked. All in a loop.

    here is a link to the code….

    Wish I had more, but as I work on this will try and get a page up for you to see. Thanks again!

    That’s enough to see how everything is structured. ??

    So, when the title and image that goes along with it (currently wrapped in an <a>) is clicked, the “hidden” <div> should toggle on and off? Will there be multiple copies of this code on the same page or only one? As I mentioned earlier, you probably want to go with jQuery. Here’s an example that should work with the code you gave:

    jQuery( document ) . ready( function( $ ) {
        $( '.show' ) . click( function() {
            $( this ) . next( '.hidden' ) . slideToggle( 'fast' );
        } );
    } );

    This essentially says, when the page is done loading, if somebody clicks on any element with the class “show”, that the following element with the class “hidden” should be toggled.

    You can put that in a separate file, upload it wherever you want (probably in your theme’s folder) then simply include it in whatever page you want it to work on. For example, if you name it author-toggle.js and put it directly in your theme’s folder (wp-content/themes/your-current-theme) then, to include it, you can add something like this to your theme’s functions.php:

    add_action( 'wp_enqueue_scripts', 'add_my_js' );
    
    function add_my_js() {
        wp_register_script( 'my-author-js', get_template_directory_uri() . '/author-toggle.js', array( 'jquery' ), false, true );
        wp_enqueue_script( 'my-author-js' );
    }

    If you wanted to only have the script load on certain pages you can wrap it up in a conditional. For example, if you only wanted it on the page with ID = 7, then:

    add_action( 'wp_enqueue_scripts', 'add_my_js' );
    
    function add_my_js() {
        if ( is_page( 7 ) ) {
            wp_register_script( 'my-author-js', get_template_directory_uri() . '/author-toggle.js', array( 'jquery' ), false, true );
            wp_enqueue_script( 'my-author-js' );
        }
    }

    Here are reference pages to some of the things I used in my examples:

    Function Reference/wp register script
    Function Reference/wp enqueue script
    Function Reference/get template directory uri
    https://api.jquery.com/slideToggle/
    https://api.jquery.com/next/

    Of course, if you plan on editing a theme, it’s always a good idea to use a child theme:

    Child Themes

    If you do use a child theme, my example for functions.php might need a little altering depending on where you upload the JavaScript file.

    Also, it was probably just lost in the move to pastebin, but make sure there’s an equals in there:

    <a href"#" class"show">

    Once you get a working page up I’ll be happy to troubleshoot any problems. I haven’t exactly tested may examples to make sure there aren’t any errors. ??

    Thread Starter scottcarlton

    (@scottcarlton)

    page is up. here

    Still not working. Right now just copy pasted as you say just to see if I could get it working. There will be multiple ones though because it is in the query post loop.

    Thanks for your help. This defiantly help me understand this alot more.

    Thread Starter scottcarlton

    (@scottcarlton)

    Doesn’t seem to be loading the js file.

    Thread Starter scottcarlton

    (@scottcarlton)

    Ok I have got it to work with this. But it opens all the hidden divs. Now I need to had a parent/child to it if I am correct.

    <script src=”https://pastebin.com/embed_js.php?i=LH7xvpyf”></script&gt;

    I had to post the code directly into the page though, because it’s not working through function.php

Viewing 15 replies - 1 through 15 (of 30 total)
  • The topic ‘Show/Hide div in loop’ is closed to new replies.