I would like to select list items based on their incrementing ID’s but I am having a bit of trouble. Thoughts?
$(document).ready(function () {
var listCount = 1;
$('#listitem-' + listCount + ' a').hover(function() {
$('#anotheritem-' + listCount).show();
return false;
});
listCount++;
});
HTML:
<ul id="cap">
<li id="listitem-1"><a href="#">content 1</a></li>
<li id="listitem-2"><a href="#">content 2</a></li>
<li id="listitem-3"><a href="#">content 3</a></li>
<li id="listitem-4"><a href="#">content 4</a></li>
</ul>
<div style="display:none;" id="anotheritem-1">hello 1</div>
<div style="display:none;" id="anotheritem-3">hello 3</div>
<div style="display:none;" id="anotheritem-3">hello 3</div>
<div style="display:none;" id="anotheritem-4">hello 4</div>
UPDATED Question. I’m trying to achieve the following revision of the answer:
$('.listitem_to_hover').hover(function () {
var senderID = sender.id.replace('listitem-', '');
$('#anotheritem-' + senderID).show();
}, function () {
var senderID = sender.id.replace('listitem-', '');
$('#anotheritem-' + senderID).hide();
});
So, you are looping through (or trying to loop through) to assign your hover function, why not use a class selector instead:
HTML:
ps: even after you edit your document.ready… you don’t have a loop?
*Okay, based on what you have shown, this should get you want you want: *