Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript ToggleClass with one id in multiple places

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #188560
    Romchick
    Participant
    
    $('#info').click(function() {
        $('.fcaption').toggleClass('open');
    });
    

    Guys, if I have multiple places with #info id, how to make it triggered only on hovered block, not to work it on all #info?

    #188561
    shaneisme
    Participant

    You can’t have multiple of the same ID’s on any page.

    Make it a class instead and you should be set.

    #188562
    __
    Participant

    Guys, if I have multiple places with #info id …

    You should not have multiple elements with the same id.
    ids must be unique.

    Consider using a class name instead.

    how to make it triggered only on hovered block, not to work it on all

    jQuery’s event functions bind the target element to the callback’s context, so the specific element that the event occurred on will be the value of this inside your callback function.

    $(".classname").hover( function(){
        this;  // the hovered element
        $( this );  // the hovered element as a jQuery object
    });
    
    #189103
    Romchick
    Participant

    Forgot to say thank you guys! The main and the only thing about “id” i really forgot – uniqueness.
    Problem solved.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.