lundi 27 janvier 2020

Is it possible to select an "a" tag by ID using Jquery

I tried .hasID in a conditional statement as below, but all my Google searches pointed to .is which does not work. The below full code changes a div class to "current" and should do two things: Swap out the content in some "divs" and change the image background in those divs.

if ($('#p-menu a').is('#link2')) { // 
       $('.home-panel').css({'background-image':'url(images/img.jpg)'});

} else if ($('#p-menu a').is('#link3')) {
       $('.home-panel').css({'background-image':'url(images/img2.jpg)'});

//Full code
$("#p-menu a").on('click', function(e) {        

        //Make a link active on click
        $('#p-menu a').removeClass('current');       
        $(this).addClass('current');

        //open content pages "<divs>"
        e.preventDefault()
        var page = $(this).data('page');
        $("#pages .page:not('.hide')").stop().fadeOut('fast', function() {
            $(this).addClass('hide');
            $('#pages .page[data-page="'+page+'"]').fadeIn('slow').removeClass('hide');
        });
        //Change backgrounds when a is set to "current" above - doesn't work! 
        if ($('#p-menu a').is('#link2')) { // also tried .hasID
            $('.home-panel').css({'background-image':'url(assets/images/backgrounds/factory-worker-using-app.jpg)'});
        } else if (('#p-menu a').is('#link3')) {
           $('.home-panel').css({'background-image':'url(assets/images/another-image.jpg)'});
        } else if (('#p-menu a').is('#link4-etc')) {
            //6 more times
        }
        $('#p-menu a').removeClass('current');
        $(this).addClass('current');

});

Thanks in advance for any help here.

Aucun commentaire:

Enregistrer un commentaire