Search code examples
jqueryfindthischildren

$(this).children / find is not working


I got a very simple code that should work. I can't understand why is not working.

I have:

<div class="mix category-1" onmouseover="showdescrmini()">
          <img src="large-464x400.jpeg" class="coverimgminipost">
          <h2 class="miniposttitle">Title</h2>
</div>

And the script:

function showdescrmini()
        {
         $(this).children('.miniposttitle').css('color','red');
        }

I already tried:

function showdescrmini()
        {
         $(this).find('.miniposttitle').css('color','red');
        }

If I put an alert() in the script, it appears when the mouse is over, so the problem is that single line.

Thanks in advance!


Solution

  • In your function, this points to window, not div element. Use onmouseover="showdescrmini(this)" and your function should look like this:

    function showdescrmini(div)
    {
             $(div).children('.miniposttitle').css('color','red');
    }