How can I use $(.class) in an appropriate way?

Viewed 41

I wanted to add some part of my main page with infinite scroll and it is working. But also I want to add this part of my page with an link. Normally it is work if my site name is like (https://example.com/index) but I don't want to redirect it. I want to make it work in (https://example.com) format. I think the problem is about jQuery selector.

    function setupPage() {

        const list = $(".list");
        if (count === 0) {
            list.append(
                `  <div> Hello</div>`);
            count++;
        }

        setTimeout("window.location = 'index#contact'", 100);

    }

     <li><a href="javascript:void(0); " onclick="setupPage()">Contact</a></li>

     <div class="list "> Content. </div>

2 Answers

You can directly set location.hash instead. Also, it is better to pass a function to setTimeout rather than a string to evaluate.

setTimeout(()=>window.location.hash = 'contact', 100);
function setupPage() {

    const list = $(".list");
    if (count === 0) {
        list.append(
            `  <div> Hello</div>`);
        count++;
    }

    setTimeout("window.location = '#contact'", 100);

}

removing index will help

Related