Search code examples
javascriptdom-events

Adding a class to a child of a button


I'm trying to work out how to add a class to a child element of a button, specifically to the <i> below:

<button><i class="icon-undo"></i> Undo</button>

Ideally, using plain JavaScript, there'd be a function which can be added to the button (i.e. <button onclick="function()">) which would add the class "icon-spin" to the child <i> of whatever element was clicked (so it could be used in multiple places in the same page).


Solution

  • Maybe the code in this question helps.... posted the answer today itself. Just pass 'this' to the function and you could do wonders to the DOM thereafter

    get nearby sibling in javascript