• Good day all,

    I’m trying to use javascript to show / hide content in a page when it is clicked,

    The code is sooo simple, the link is like this:

    <a href="javascript:;" onmousedown="toggleDiv('govt');">Government, Services &amp; Schools</a>

    and the details which appear when clicked, sit in a div like this:

    <div id="govt" class="fix" style="display: none">
    <ul><li></li></ul>
    </div>

    But when I save the page WP always changes the <div>s to <p>s and even closes the </p> prematurely, which kills the whole thing.

    Does anyone know if divs aren’t allowed?

    Is there a workaround?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter dominoeffect

    (@dominoeffect)

    I should probably point out that the script is working fine, i have tested it in plain html and it is also used in header.php for the archives menu where it works grand, it’s just that page writer is stripping my divs!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    No, DIV’s are not allowed. Use a P instead. It’ll be functionally the same thing for a display:none case. Or in your specific case, put the class and such onto the UL directly. A DIV is unnecessary.

    And realistically, you probably shouldn’t be putting complex HTML and javascript into Post (or Page) contents like that. If you want complex HTML, put it into a Page Template instead. Only put actual content (like text and images) into the Post/Page content.

    WordPress makes the implicit assumption that what you are posting is content. HTML and effects and other display tricks are not content, they are what is known as “Presentation”. The presentation should be separated from the content.

    Why should it be separated? Many reasons, the biggest of which is that you may want to publish your content in more than one format. The “feed” idea is the most obvious example of this, but not the only one. The point is that the content is the base form of the material that you are publishing. Javascript to hide things is not the actual material you are publishing, it’s how you display that material. Display should be left to the theme itself, that’s the separation. The theme controls the display of the material. In theory, you should be able to change your theme and everything will still work instantly, although it may look and work differently. That’s the whole point of the separation.

    In your case, I’d make a Page Template and put the DIV into that, along with the necessary javascript. Inside the Page content, I’d only put the essence of the material, what will be displayed inside that DIV.

    Perhaps you are thinking that only some of it will be hidden and some will be displayed, like a spoiler. In which case I’d say to have some special tags to put in your Page and then code the Page Template to show it accordingly. For example, a “spoiler” could be a normal P paragraph that gets a class of “spoiler”. Then your javascript (or Page Template) can act on that accordingly. This way, if you switch themes, your content still displays. Maybe not exactly how you wanted for this theme, but it still displays.

    That’s the general idea, anyway. Semantic markup: The content has tags that describe it, not tags that define its display characteristics, because those characteristics can then be easily changed when they’re defined elsewhere.

    Thread Starter dominoeffect

    (@dominoeffect)

    Hi Otto,

    Thank you for the help. Sorry it’s taken me so long to reply, I haven’t had the time to get into this over holiday period.

    I appreciate your clarification between display attributes and content and am rethinking what I was doing. Btw, putting the class onto a UL solved perfectly what i was asking, thanks!

    The page in mind is actually a directory of local services for a website where I will be blogging to that community, I had split the services into groups eg: Government, Services & Schools, Local Health Services, Transport, Travel & Tourism etc; and listing their individual details meant fora long page where a lot of page scrolling was required!

    By hiding each list under their group titles it makes it easier for the user to find what they’re looking for faster- which may be the latest train times, the number for their local doctor, primary school etc.

    Thanks again for increasing my understanding of WP, I am thinking now of perhaps just splitting it into individual pages for each group, and using parent pages to manage them.

    M.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘no divs allowed?? [in page & post code]’ is closed to new replies.