I want that when passing the mouse for a Li, the result appears in the DIV

Asked

Viewed 160 times

3

I want that by passing the mouse by a li, the result appears in div

placing the mouse on the Son 2: example: Father 1 > Son 2

or in the Father 2: Father 2

<li ><a href="#estaEm">Pai 1</a>
  <ul>
    <li ><a href="#estaEm" >Filho 1</a></li>
    <li ><a href="#estaEm" >Filho 2</a></li>
  </ul>
</li>
<li ><a href="#estaEm">Pai 2</a>
  <ul>
    <li><a href="#estaEm" >Filho 1</a></li>
  </ul>
</li>
</div>
<div id="estaEm" value ="">resultado </div>

The jQuery I made doesn’t work in case only Father 2 appears:

$( "li" ).bind( "mouseover", function() {
    var result=''

    $( this ).find("a").each(function () {
        result += " > " + $( this ).text()
    });

    $("#estaEm").text(result)
}); 
  • I didn’t quite understand your question, you would like something like this: https://jsfiddle.net/k214LLdj/ try to be clearer.

1 answer

3

Use $('li > a') as selector in jQuery to catch the child(s) from li and use this and take the text from li that the mouse passed, it is quite simple.

Behold:

$('li > a').mouseover(function() {
  $('#estaEm').text($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
   <li ><a href="#estaEm" >Filho 1</a></li>
   <li ><a href="#estaEm" >Filho 2</a></li>
</ul>
</li>
<li >
   <a href="#estaEm">Pai 2</a>
   <ul>
      <li><a href="#estaEm" >Filho 1</a></li>
   </ul>
</li>
</div>
<div id="estaEm" value ="">resultado </div>

  • It is wrong to pass the mouse on the father 2 It appears father 2 son 1 It should only be father 2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.