Jeremy Karlsson
Jeremy Karlsson

Reputation: 1089

Change class of element in div when div is focused?

How do change class of an element inside a div when the parent div is focused?

Like when I am active in the

<div id="window" class="fenster"><a class="bye"></a></div>

The class of A changes to hello?

Upvotes: 1

Views: 713

Answers (2)

Mike Hunt
Mike Hunt

Reputation: 19

You can do this without jquery:

document.getElementById('window').setAttribute('class', 'newclassname');

Upvotes: 1

Richard Dalton
Richard Dalton

Reputation: 35793

Assuming by focused you mean mouse is over it and you can use jQuery?

$('#window').mouseover(function() {
    $('a.bye', this).removeClass('bye').addClass('hello');
});

And if you want to reset the class when you move the mouse out:

$('#window').hover(function() {
    $('a.bye, a.hello', this).toggleClass('bye').toggleClass('hello');
});

JSFiddle Example

JSFiddle Example using only javascript

Upvotes: 2

Related Questions