Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How can I create a button that displays content within a div?

Malcolm

Administrator
Administrator
Staff Team
Code Plus
Company Plus

Hello All!​

I'm developing a resume-website and I've gotten it pretty much where I need to. But there's one last step I need to work on and it's the content and how to display it. I decided that I was going to use buttons and when you click them it would show the content.
E.g.: Click About Me > Content area shows about me content.

I've done a bit of research and read something about an "OnClick", but I tried the examples from what I found online and have not gotten the result I want. My goal is that when I click on a button, it will display the content within the content area as I mentioned above. Can someone please point me in the right direction?

Below is what I want:
 
You could give an id for each div that represents a section (Ex. id="section1"). Each div that is not currently displayed has the style display: none;.
You create a function that takes the id of a div in argument and applies it the style display: inline-block (Making it appear) and sets the div that was displayed with the style display: none; (It saves the number of the currently displayed div in a variable).
If the function is named displaySection, you could give the attribute onClick="displaySection(1)" to the first button, onClick="displaySection(2)" to the second one...

I do not know if my explanation is clear or not :laugh:.
 
That was kinda what I was thinking I'll give it a try. I may have to start another project and just build something with tabs.
 
Hi @Malcolm!

Place buttons of your choice in the column to the left. Place an "iframe" to the right of them and assign a unique "id" attribute to it. Then setup each button onclick event attribute as:

HTML:
<... onclick="document.getElementById('...iframe_id...').src='file_of_your_choice.html'" ...>

You'll have to make several html content files, each corresponding to specific button, which will open inside the iframe upon clicking the buttons.

[edit]
If you really want a div content version, I'd recommend a navbar to the left, and static div contents to the right. You'd have to make several htmls, each with the same navbar, but with a different div content. There are other options with div (like setting innerHTML property in script for example, or even harder - manipulating DOM) but this is the most SEO friendly version I can think of.
 
Last edited:
I did something similar for my pen and paper ruleset, there the website is delivered via GitLab, there also only HTML, CSS and JS works.

I use Bootstrap.
The button:
HTML:
<a class="nav-link" onclick="showHome()"><i class="fas fa-home"></i></a>

JS code:
JavaScript:
function showHome() {
    $("#content").load("templates/home.html");
}
this will load the content to be displayed from the file
 
Hi all! Still working on this, @ivan.moony I tried with your solution first but seems to have few kinks (appearance & performance) I have to work out such as load time. It appears that having an iframe uses a bit of memory which is causing performance issues. I'm still looking into it.

I'm going to keep trying your solutions guys! Thank you so much!
 
Back
Top Bottom