/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Returning a visible DIV

[code]<html>
<head>
<script type=”text/javascript”>
function listPages()
{
var i = 0; var page, currentPage;

while(i < 25)
{
i++;
if(document.getElementById(“page” + i) != null)
{
if(document.getElementById(“page” + i).style.display != “none”)
currentPage = i;
}
}

alert(currentPage);
}
</script>
<style type=”text/css”>
.page
{
display: none;
}
</head>

<body>
<div id=”page1″>Page 1</div>
<div id=”page2″ class=”page”>Page 2</div>
<div id=”page3″ class=”page”>Page 3</div>
<a href=”javascript:nextPage();”>Go to the next page</a>
</body>
</html>[/code]

Obviously I didn’t include all my code, which is very messy and extraneous to the question. The trouble is, i returns as undefined. I want i to return as the “current page,” which is the page that is currently displayed. As you cycle through the pages, the currently displayed page should be i. When you first load the page, i should return 1 since the page1 DIV is not hidden, but it never returns true.

Why? Can someone help me with this? It’s extremely frustrating.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 24.2009 — Based only on the information you provided:

  • 1. There is no closing </style> tag.

  • 2. The listPages() function is not called anywhere.
  • Copy linkTweet thisAlerts:
    @JMRKERFeb 24.2009 — Only a guess as to what you are trying to accomplish.
    <i>
    </i>&lt;html&gt;
    &lt;head&gt;
    &lt;script type="text/javascript"&gt;
    var maxPages = 3;
    function listPages() {
    var i = 0;
    var flag = 0;
    while(i &lt; maxPages) {
    i++;
    if (document.getElementById("page" + i) != null) {
    if (document.getElementById("page" + i).style.display != "none") {
    flag = i; break;
    }
    }
    }
    if (flag == 0) { flag = 1; }
    return flag;
    }
    function nextPage(direction) {
    var i = listPages();
    document.getElementById('page'+i).style.display = 'none';
    if (direction &lt; 0) {
    if (i &gt; 1) { i--; }
    } else {
    if (i &lt; maxPages) { i++; }
    }
    document.getElementById('page'+i).style.display = 'block';
    }
    &lt;/script&gt;
    &lt;style type="text/css"&gt;
    .page {
    display: none;
    }
    &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;div id="page1"&gt;Page 1&lt;/div&gt;
    &lt;div id="page2" class="page"&gt;Page 2&lt;/div&gt;
    &lt;div id="page3" class="page"&gt;Page 3&lt;/div&gt;
    &lt;br&gt;
    &lt;a href="javascript:nextPage(-1);void(0)"&gt;Go to the prev page&lt;/a&gt;
    &lt;a href="javascript:nextPage(1);void(0)"&gt;Go to the next page&lt;/a&gt;
    &lt;p&gt;
    &lt;button onclick="alert(listPages())"&gt;List Pages&lt;/button&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @lupus6x9authorFeb 25.2009 — JMRKER:

    Wow, this is exactly what I was looking for. I was writing the script partially by myself and partially copying and pasting from other sources, so my overall code was probably 200 times this size. This does the same thing but at a fraction of the size. I only have one question left:

    How could I, say, insert a strikethrough over the links if you cannot go further? (e.g. "Go to the prev page" becomes "<s>Go to the prev page</s>" if we're on page 1 and the <s> tags around "Go to the next page" if we're on the max number of pages.)

    I'm thinking of a way within the function of nextPage of changing the innerHTML of a <div> that would contain the links.


    [b]EDIT:[/b] Solved myself.

    Thanks for the help!
    Copy linkTweet thisAlerts:
    @JMRKERFeb 25.2009 — EDIT: Solved myself.[/QUOTE]

    Glad I was able to help so quickly! ?????
    ×

    Success!

    Help @lupus6x9 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...