Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8149395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:50:34+00:00 2026-06-06T14:50:34+00:00

The complete code is copied and pasted below. I am new to javascript and

  • 0

The complete code is copied and pasted below. I am new to javascript and jquery and am not sure why the following is happening:

Suppose I am at URL http://someurlhere/#1 in browser Google Chrome.
If I go to the address bar delete just the number 1 in the above URL, type 2 and press enter, the page does not navigate to section with id=2. Now, if I go the address bar again and just press enter, it will navigate to section with id=2. Why does it not navigate the first time I pressed enter?

I have been searching and I thought this probably has something to do with hashchange event. I added the last few lines in the script. Whenever I change the id number I do get a message in console, but the above described behavior remains unchanged. Can someone explain why pressing enter does not work the first time, but does work the second time and how can I fix that? Thank you.

The code:

<html>
<head>
    <title>Selecting multiple DIV tags with jquery</title>
    <script type="text/javascript" src="./jquery.js">
    </script>
    <style type="text/css">

        html{
            overflow: hidden;
        }
        .slide {
            display: block;
            position: absolute;
            border-style: solid;
            border-width: 1px;
            top: 0px;
            left: 0px;
            padding:20px;
            height: 95%;
            width: 100%;
        }
    </style>
</head>
<body>

    <br/><br/><br/><br/><br/><br/>
    <section class="slide">This is the first div.</section>
    <br/><br/><br/><br/><br/><br/>
    <section class="slide">
        This is the second div.


    </section>

    <br/><br/><br/><br/><br/><br/>
    <section class="slide">This is the third div.</section>
    <br/><br/><br/><br/><br/><br/>
    <section class="slide">This is the fourth div.</section>
    <br/><br/><br/><br/><br/><br/>
    <section class="slide">This is the fifth div.</section>
    <br/><br/><br/><br/><br/><br/>
    <section class="slide">This is the sixth div.</section>



    <script type="text/javascript">

        // Assign ids to each section in the order they appear.
        $("section").each(function(index){
            $(this).attr('id', index+1);
            $(this).append('<button onClick="nextdiv();">Some div</button>');
            $(this).css('opacity', 0);
        });

        // Check if the current url points to a specific id. If not point
        // it to id = 1, otherwise point it to the id specified in the URL.
        var currenturl = $(location).attr('href');

        var indexhash = currenturl.lastIndexOf('#')

        if (indexhash === -1){
            var newurl = currenturl + '#1';
            $("#1").css('opacity',1);
            window.location.href = newurl;
        }
        else {
            var currentid = currenturl.substring(indexhash, currenturl.length);
            console.log(currentid);
            $(currentid).css('opacity', 1);
            window.location.href = currenturl;
            // window.location.assign(currenturl);
        }



        var newurlid = function(){
            var currenturl = $(location).attr('href');
            var indexhash = currenturl.lastIndexOf('#');
            var currentid = currenturl.substring(indexhash+1, currenturl.length);
            var newid = parseInt(currentid, 10) + 1;
            var newurl = currenturl.substring(0,indexhash+1) + newid;
            return {'newurl': newurl, 'newid': newid}
        };


        nextdiv = function(){
            console.log(newurlid().newurl);
            var newid = parseInt(newurlid().newid);
            console.log(newid);
            var selectid = '#' + newid;
            $("section").each(function(index){
            $(this).css('opacity', 0);
            });
            $(selectid).css('opacity', 1);
            window.location.href = newurlid().newurl;
        };


        $(window).bind('hashchange', function() {
                var currenturl = $(location).attr('href');
                console.log(currenturl);
                window.location.href = currenturl;
        });

    </script>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:50:35+00:00Added an answer on June 6, 2026 at 2:50 pm

    ID’s cannot be numbers, they need to start with characters. Try something like tab1, tab2, tab3.

    As for the hash change, you are very close. What you’re doing at the moment is parsing the hash on a page refresh. You also have a hash change event binded, but it doesn’t do anything at the moment. You need to move most of your hash parsing code into a function like this:

    var hashChange = function() {
    
    // Check if the current url points to a specific id. If not point
            // it to id = 1, otherwise point it to the id specified in the URL.
            var currenturl = $(location).attr('href');
    
            var indexhash = currenturl.lastIndexOf('#')
    
            if (indexhash === -1){
                var newurl = currenturl + '#1';
                $("#1").css('opacity',1);
                window.location.href = newurl;
            }
            else {
                var currentid = currenturl.substring(indexhash, currenturl.length);
                console.log(currentid);
                $(currentid).css('opacity', 1);
                window.location.href = currenturl;
                // window.location.assign(currenturl);
            }
    
    
    
            var newurlid = function(){
                var currenturl = $(location).attr('href');
                var indexhash = currenturl.lastIndexOf('#');
                var currentid = currenturl.substring(indexhash+1, currenturl.length);
                var newid = parseInt(currentid, 10) + 1;
                var newurl = currenturl.substring(0,indexhash+1) + newid;
                return {'newurl': newurl, 'newid': newid}
            };
    
    
            nextdiv = function(){
                console.log(newurlid().newurl);
                var newid = parseInt(newurlid().newid);
                console.log(newid);
                var selectid = '#' + newid;
                $("section").each(function(index){
                $(this).css('opacity', 0);
                });
                $(selectid).css('opacity', 1);
                window.location.href = newurlid().newurl;
            };
    
    };
    

    And call it out on the hashchange event and when the DOM is ready, so basically

     $(document).ready(hashChange);
     $(window).bind('hashchange', hashChange);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is the complete code of the jquery AJAX tab script I am working
I'm not particularly good with jQuery, so a complete code solution would be ideal.
In most of the videos, I see expert JQuery developers writing complete code for
I am trying to do this: List<Parent> test = new List<Child>(); The complete code
This is the complete source code: http://www.sendspace.com/file/lwxpyf I have a problem with a JSF
I'm trying to utilize ZeroClipboard (http://code.google.com/p/zeroclipboard/wiki/Instructions) to copy the current URL to the user's
I am trying Jquery Autocomplete element.In code I can see following import and references
We have a complete code for getting the values from PHP through Jquery AJAX
Below is the my complete code Here is cfg file <hibernate-configuration> <session-factory> <!-- Database
I am trying to build an openssl simple program. Here is the complete code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.