Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 73905

Is it possible to directly go to content of div by URL?

$
0
0

Content is displayed with the previous and next buttons, but can it be possible to show the contents of the box directly with the link?

For example:

https://example.com/content/#/2

so can the second box be shown to the content?

$(document).ready(function() {
  $('#next').click(function() {
    if ($('.active').next('.case').length) {
      $('.active').removeClass('active')
        .next('.case')
        .addClass('active');
    }
  });
  $('#prev').click(function() {
    if ($('.active').prev('.case').length) {
      $('.active').removeClass('active')
        .prev('.case')
        .addClass('active');
    }
  });
});
.case {
  display: none
}

.active {
  background: yellow;
  border: 1px solid;
  display: block
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="next"><h1>next</h1></div><div id="prev"><h1>prev</h1></div><div id="box1" class="case active">content 1</div><div id="box2" class="case">content 2</div><div id="box3" class="case">content 3</div>

What have I done?

if($(location).attr('hash') != null) {
        document.getElementById('box' + $(location).attr('hash').replace("#", "")).style.display = 'block';
    }

but it didn't work

Update 1

I changed:

if($(location).attr('hash') != null) {
        document.getElementById('box' + $(location).attr('hash').replace("#/", "")).style.display = 'block';
        document.getElementById('box' + $(location).attr('hash').replace("#/", "")).addClass('active');
    }

result, it didn't work

JSFiddle on Demo

If possible, can you guide me?


Viewing all articles
Browse latest Browse all 73905

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>