Chris
Chris

Reputation: 949

Applying "this" to a child element

Instead of repeating a function for every ID of my elements, I would like to use "this" to adjust the CSS of an internal element. For instance, this is how far I've gotten (doesn't work).

$(".parent").hover(function() {
$("this").find(".child").css("height","150px")
});

How can I become a more efficient coder and use "this"?

Upvotes: 0

Views: 64

Answers (3)

nicksweet
nicksweet

Reputation: 3999

Heres a solution that does not require jQuery:

someElement.addEventListener('mouseover', function(e) {
    var children = Array.prototype.slice.call(e.target.children);

    children.forEach(function(child) {
        child.style.height = '150px';
    });
}, false);

Upvotes: 0

Ian
Ian

Reputation: 6124

Change it to

$(".parent").hover(function() {
      $(this).find(".child").css("height","150px")
});

So that there are no quotes.

Upvotes: 2

cardern
cardern

Reputation: 725

Remove quotations from "this" and it'll work. A common mistake :)

code like this:

$(this).find(".child").css("height","150px")

Upvotes: 3

Related Questions