Attempting to apply .hover to a class that shows a div based on a dynamic ID. For example:
HTML
<div id="parent_one">
<div class="touch" id="one">Touch me!</div>
<div id="t_one"></div>
</div>
<div id="parent_two">
<div class="touch" id="two">Touch me!</div>
<div id="t_two"></div>
</div>
jQuery
$('.touch').hover(
function(){
var id = $(this).attr('id');
var value = '#t_' + id;
$(value).fadeIn(800);
},
function(){
var id = $(this).attr('id');
var value = '#t_' + id;
$(value).delay(1500).fadeOut(800);
}
);
I have a feeling my mistake is in the use of $(this) but can’t nail it down.
This:
Needs to include
#:Otherwise you are just selecting for an element with the tag
t_one, which is not an existing tag name.However using ids for this is pretty ugly. If you have consistent HTML like this, why not use a class structure like this:
with JS like this: