1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to keep buttons within their respective divs

Discussion in 'HTML & Website Design' started by ketting00, Nov 13, 2024.

  1. #1
    Hi guys,

    I've blocks of divs which look like below:
    
    <div class="main-container">
        <div id="tabs">
            <a href="#tab1!" class="tabs-link link1">
                <i class="fab fa-tab1">Explore</i>
            </a>
            <a href="#tab2!" id="follow" class="tabs-link link2">
                <i class="fab fa-tab2">Follow</i>
            </a>
        </div>
        <div class="tabs-item item1" id="explores">
            <div class="tabs-line line1" id="content1">
                <!-- DATA -->
            </div>
            <center><button id="load-more1" class="load-more-button">Load More</button></center>
        </div>
        <div class="ptabs-item item2" id="follows">
            <div class="tabs-line line2" id="content2">
                <!-- DATA -->
            </div>
            <center><button id="load-more2" class="load-more-button">Load More</button></center>
        </div>
    </div>
    
    Code (markup):
    All the buttons are hidden by CSS "display: none;", they are shown by javascript when the visitor visits the page and if there are more data to display.

    However, after the buttons are displayed by JavaScript they are there even when I toggled the tab. The button 1 appeared on the top when I switched to tab 2. Button 2 appeared at the bottom when I switched to tab 1.

    How can I fix this?

    Thank you,
     
    ketting00, Nov 13, 2024 IP
  2. kennedygitahi

    kennedygitahi Active Member

    Messages:
    37
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    Articles:
    2
    #2
    Can you also post the JavaScript?
     
    kennedygitahi, Nov 13, 2024 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #3
    Hi @kennedygitahi,

    It was just an ordinary fetch API.
    
    const b1 = document.getElementById('load-more1');
    const b1 = document.getElementById('load-more2');
    
    async function getData() {
        const dt = "/path/to/file.json";
        fetch(dt, {
            method: 'POST',
            body: value
        }).then(response => {
            return (response.text());
        }).then((data) => {
            b1.style.display = 'block';
        }).catch(() => {
            console.log("No Data");
        });
    }
    
    Code (markup):
    It triggered when you clicked on the link button. Currently, I fixed this with b1.style.display = 'none'; when you clicked on another tab. But it isn't the right way. The button disappeared unless you refresh the page. I want to fix this with CSS but I don't know how to do it.
     
    ketting00, Nov 14, 2024 at 12:50 AM IP
  4. mayazir

    mayazir Member

    Messages:
    193
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    35
    #4
    ask GPT and analyze its answer
     
    mayazir, Nov 14, 2024 at 3:04 PM IP
  5. GreenHost.Cloud

    GreenHost.Cloud Member

    Messages:
    335
    Likes Received:
    25
    Best Answers:
    3
    Trophy Points:
    33
    #5
    ChatGPT version:

    document.querySelectorAll('.tabs-link').forEach(link => {
        link.addEventListener('click', function(event) {
            event.preventDefault();
    
            // Hide both load more buttons first
            document.getElementById('load-more1').style.display = 'none';
            document.getElementById('load-more2').style.display = 'none';
    
            // Get the id of the tab we are switching to
            const targetTab = this.getAttribute('href').substring(1);
    
            // Show the content and load more button for the active tab
            document.querySelectorAll('.tabs-item').forEach(item => {
                item.style.display = 'none'; // Hide all tab content
            });
    
            // Show the active tab content
            document.getElementById(targetTab).style.display = 'block';
    
            // (Optional) Show the corresponding "Load More" button if necessary
            if (targetTab === 'explores') {
                document.getElementById('load-more1').style.display = 'block';
            } else {
                document.getElementById('load-more2').style.display = 'block';
            }
        });
    });
    Code (CSS):
     
    GreenHost.Cloud, Nov 18, 2024 at 9:24 AM IP
  6. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #6
    ketting00, Nov 18, 2024 at 11:19 AM IP