Don Simo Don Simo - 4 months ago 8
Javascript Question

change text with Id by selecting an image with Id

I have a query that make changes on list item text while the mouse is over an image, I put an event target within the query :

$('img').on('mouseover', function(){
// I would like an Id of a text instead of the 'li'
$('li').css('text-decoration', 'underline');
});

$('img').on('mouseout', function(){
$('li').css('text-decoration', 'none');
});


but I want that change to be applied to only the item wich have the same Id as the image :

s[i++] = '<li id=\"'+ vizList[j].name +'\">';
s[i++] = '<a>'+ vizList[j].name + '</a>';
s[i++] = '<img id=\"'+ vizList[j].name +'\" src="../renderer/bundles/' + vizList[j].icon + '" width="268" height="120" style="display:block"/>';
s[i++] = '</li>';

Answer

Is the below code what you expect?

$('img').on('mouseover', function(event){
  $(event.target).closest('li').css('text-decoration', 'underline');
});

$('img').on('mouseout', function(event){
  $(event.target).closest('li').css('text-decoration', 'none');
});
Comments