Forums

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

Home Forums JavaScript hover effect on divs with same id

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #166320
    mashcom
    Participant

    hi, i have been generating divs using php, These div have the same IDs, so i was wondering how i can use jquery to do a certain action on a div focused on rather than all the divs responding.. I have tried a number of snippets but all have failed, 1 snippet was working on the first div only and the other was working on all div, i just want the action to be done on one div at a time (HOVER)

    #166324
    Paulie_D
    Member

    To start with you can’t re-use ID’s on the same page.

    Each div would have to have a unique ID.

    So that might be your problem.

    The divs can have a common class and once you have achieved that we can move on.

    Depending on what the hover action is supposed to be it’s possible you don’t need JS/JQ at all.

    #166472
    Chromawoods
    Participant

    Something like this? http://codepen.io/chromawoods/pen/zKdwk/

    Like @Paulie_D said, you should never use the same ID attribute more than once on a page. However, when capturing an event with jQuery, you can target the specific element using $(this), even if your original jQuery selection was targeting a whole class of elements.

    In my example, I make a selection on any .box element but then I use $(this) to add/remove a CSS class on the currently hovered box. You can do whatever you need to the targeted element.

    #166485
    mashcom
    Participant

    thank you guys, you jus made my day, ;-)

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