Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
How to make custom URLs for different website states?

In my project I have a page where there are different directories inside other directories, which show and hide with "style.display="block/none", problem is that I cannot figure out how to save those states in an URL so they can be sent to others.

Ideally after user clicks on the button to "Show Section 1", and then on "Show section 11" the URL becomes website.com/section1/section11 that he can share to others.

So question is, how do I save states of websites to be shared based on where in the directory the user is?

Example code:

JS

const Section1 = document.getElementById("section1");
const Section2 = document.getElementById("section2");
const Section11 = document.getElementById("section1-1");
const Section12 = document.getElementById("section1-2");

function show1(){
     Section1.style.display="block";
     Section2.style.display="none";
}

function show2(){
     Section1.style.display="none";
     Section2.style.display="block";
}


function show11(){
     Section11.style.display="block";
     Section12.style.display="none";

}

function show12(){
     Section11.style.display="none";
     Section12.style.display="block";

}

And so on...

HTML


<button onclick="show1()">Show Section 1</button>
<button onclick="show2()">Show Section 2</button>

<div id="section1" style="display: block">

     <button onclick="show11()">Show Section 11</button>
     <button onclick="show12()">Show Section 12</button>

     <div id="section1-1" style="display: block">

          <p>Some text</p>
          <button onclick="show111()">Show Section 111</button>
          <button onclick="show112()">Show Section 112</button>

     </div>

     <div id="section1-2" style="display: none">

          <p>Some text</p>
          <button onclick="show121()">Show Section 121</button>
          <button onclick="show122()">Show Section 122</button>

     </div>

</div>

<div id="section2" style="display: none">

<p>Something in here...</p>

</div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try something like this:

var url = ""

function show(target) {
  $(target).siblings("[id^=section]").hide();
  $(target).show();

  generateUrl();
}

function generateUrl() {
  url = "/";
  url += $("[id^=section]:visible").map(function() {
    return $(this).attr("id")
  }).get().join("/");
  console.log(url)
}

Since you tagged jquery then I thought I would provide you an example with less code. I will work properly once your html is complete.

But it general it generates the url based on the div's where the id start with section and it's visible.

Demo

var url = ""

function show(target) {
  $(target).siblings("[id^=section]").hide();
  $(target).show();

  generateUrl();
}

function generateUrl() {
  url = "/";
  url += $("[id^=section]:visible").map(function() {
    return $(this).attr("id")
  }).get().join("/");
  console.log(url)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="show('#section1')">Show Section 1</button>
<button onclick="show('#section2')">Show Section 2</button>

<div id="section1" style="display: block">

  <button onclick="show('#section1-1')">Show Section 11</button>
  <button onclick="show('#section1-2')">Show Section 12</button>

  <div id="section1-1" style="display: block">

    <p>Some text</p>
    <button onclick="show('#section1-1-1')">Show Section 111</button>
    <button onclick="show('#section1-1-2')">Show Section 112</button>

  </div>

  <div id="section1-2" style="display: none">

    <p>Some text</p>
    <button onclick="show('#section1-2-1')">Show Section 121</button>
    <button onclick="show('#section1-2-2')">Show Section 122</button>

  </div>

</div>

<div id="section2" style="display: none">

  <p>Something in here...</p>

</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!