- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
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
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)
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.
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.
thank you guys, you jus made my day, ;-)