Sami Sami - 10 months ago 57
Javascript Question

How to append a child to one of the elements in a variable by part of the class name and replace the parent?

I have a JS variable

var e
which is created by
method. I want to append a new element to an element inside the
which has
in its class name (full class name is
large button 1 jackson
). Also, how can I replace the
large button 1 jackson
element with the updated element afterwards? Would you suggest a solution?


Here is an example using querySelector

    // container element
    var e = document.createElement('div');
    // child element with class 'large button 1 jackson'
    e.innerHTML = '<div class="large button 1 jackson">some text</div>';
    // insert the container element
    // new element
    var newElem = document.createElement('textarea');
    // find a element with jackson class (using .jackson as selector)
    var jackson = e.querySelector('.jackson');
    // insert new element