1

The filter on my website needs to have a default value on load. My main JS script uses this, in case it's useful for the solution: window.addEventListener('DOMContentLoaded', event => { //code to be loaded }

The filter has no default, but I want to set it to one of the data-rel values by default so that the page loads with the content already filtered with my defined default.

I looked around for a while at other similar problems but couldn't find a solution to my exact problem.

In a separate filter JS file, I'm using this code as a base:

JS:

$(function(){
    var selectedClass = "$('*[data-rel="web"]');";
    $(".filter").click(function(){
        selectedClass = $(this).attr("data-rel"); 
        $(".work").fadeTo(100, 0.1);
        $(".portfolio-item").not("."+selectedClass).fadeOut().removeClass('scale');
        setTimeout(function(){
            $("."+selectedClass).fadeIn().addClass('scale').sort();
            $(".work").fadeTo(300, 1);
        }, 300); 
    });
});

HTML:

<section class="no-padding" id="portfolio">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h2 class="section-heading">Portfolio</h2>
                <h3 class="section-subheading text-muted">A showcase of my work.</h3>
                <div class="tabs">
                    <button class="btn filter" id="filt" href="" data-rel="all">ALL</button>
                    <button class="btn filter" id="filt" data-rel="web">Web Design</button>
                    <button class="btn filter" id="filt" data-rel="graphics">Graphic Design</button>
                    <button class="btn filter" id="filt" data-rel="branding">Branding</button>
                    <button class="btn filter" id="filt" data-rel="photography">Photography</button>
                    <button class="btn filter" id="filt" data-rel="motion">Motion Graphics</button>

                </div> 
                <br>
            </div>
            <div class="work">    
                <!--div class="row"-->
                    <div class="col-md-4 portfolio-item web scale tile all">
                        <a href="#">
                            <img class="img-responsive" src="http://placehold.it/700x400" alt="">
                        </a>
                        <h3>
                            <a href="#">Project Name 1</a>
                        </h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
                    </div>
                    <div class="col-md-4 portfolio-item graphics scale  tile all">
                        <a href="#">
                            <img class="img-responsive" src="http://placehold.it/700x400" alt="">
                        </a>
                        <h3>
                            <a href="#">Project Name 2</a>
                        </h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
                    </div>
                    <div class="col-md-4 portfolio-item branding scale tile all">
                        <a href="#">
                            <img class="img-responsive" src="http://placehold.it/700x400" alt="">
                        </a>
                        <h3>
                            <a href="#">Project Name 3</a>
                        </h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
                    </div>
                <!--/div-->
                <!-- /.row -->

                <!-- Projects Row -->
                <!--div class="row"-->
                    <div class="col-md-4 portfolio-item motion scale tile all">
                        <a href="img/portfolio/fullsize/1.jpg">
                            <img class="img-responsive" src="http://placehold.it/700x400" alt="">
                        </a>
                        <h3>
                            <a href="#">Project Name 4</a>
                        </h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
                    </div>
                    <div class="col-md-4 portfolio-item motion scale tile all">
                        <a href="#">
                            <img class="img-responsive" src="http://placehold.it/700x400" alt="">
                        </a>
                        <h3>
                            <a href="#">Project Name 5</a>
                        </h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
                    </div>
                    <div class="col-md-4 portfolio-item motion scale tile all">
                        <a href="#">
                            <img class="img-responsive" src="http://placehold.it/700x400" alt="">
                        </a>
                        <h3>
                            <a href="#">Project Name 6</a>
                        </h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
                    </div>
                <!--/div-->

                <!-- Projects Row -->
                <!--div class="row"-->
                    <div class="col-md-4 portfolio-item photography scale tile all">
                        <a href="#">
                            <img class="img-responsive" src="http://placehold.it/700x400" alt="">
                        </a>
                        <h3>
                            <a href="#">Project Name 7</a>
                        </h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
                    </div>
                    <div class="col-md-4 portfolio-item graphics scale tile all">
                        <a href="#">
                            <img class="img-responsive" src="http://placehold.it/700x400" alt="">
                        </a>
                        <h3>
                            <a href="#">Project Name 8</a>
                        </h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
                    </div>
                    <div class="col-md-4 portfolio-item web scale tile all">
                        <a href="#">
                            <img class="img-responsive" src="http://placehold.it/700x400" alt="">
                        </a>
                        <h3>
                            <a href="#">Project Name 9</a>
                        </h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
                    </div>
                </div>
            <!--/div--> <!--WORK-->
            <!-- /.row -->
            <hr>
        </div>
    </div>
</section>

For this example, I would set "web" as the default (of course, "all" isn't the default, but appears that way as the code just shows all items on the page before a filter is clicked anyway).

I tried making selectedClass = $(.filter).attr("data-rel", "web"); or simply selectedClass = "web";, and various other variants such as $(.filter).attr({"data-rel": "web"}); but none of that works. I believe I'm missing something in regards to how the click function is interacting with the data-rel selection.

I also tried adding this after the initial declaration of selectedClass:

$(".portfolio-item").not("." + "selectedClass").removeClass('scale');
  setTimeout(function () {
    $("." + selectedClass).addClass('scale').sort();
    $(".work");
  }, 1)

The idea being to copy what is done when a button is clicked but get rid of the fading and make it as instantaneous as possible. I also tried wrapping that part in $(document).ready(function(){}. But none of that did anything.

I'd appreciate it if someone could please explain to me what I'm doing wrong so that the default filter is "web" when the page loads.

2
  • 1
    You can add $('[data-rel="web"]').trigger('click') for triggering the click event on page load and can use .portfolio-item{ display:none;} in css for hiding all element by default . See this example.
    – Swati
    Commented Jan 24, 2023 at 4:20
  • Ah, that does the trick, thanks very much! I do wonder if there is a solution that loads like that rather than requiring it to trigger a click event on load though. Also, I noticed that using let or const instead of var prevents the script from working. Not sure why that is though. I would have expected let to work, at least.
    – Twofreid
    Commented Jan 24, 2023 at 5:53

0

Browse other questions tagged or ask your own question.