javascriptgoogle-closuregoogle-closure-library

removing an appended element using javascript/closure?


I currently have a click event in place that when selected appends a search box to .header, this is done using google closure. My problem now is if I click a close button I want to remove this appended element. I know using jQuery requires only .remove() but Im unsure how to achieve this in closure or vanilla js. Can anyone advise how I can do this?

Current code:

if(goog.dom.getElementsByClass('pe')){
    var searchCtn = goog.dom.getElementsByClass('search');     
    var headerWrapper = goog.dom.getElementByClass('header');     
    goog.dom.append(headerWrapper,searchCtn); 
} 

var closeButton = goog.dom.getElement('close');
    goog.events.listen(closeButton, goog.events.EventType.CLICK, function() {
    console.log('Remove appended');
}, false, this); 

Solution

  • The function is this:

    goog.dom.removeNode = function(node) {
      return node && node.parentNode ? node.parentNode.removeChild(node) : null;
    };
    

    So the code is like below(assume the search box is the parent of the close button):

    goog.events.listen(closeButton, goog.events.EventType.CLICK, function() {
        goog.dom.removeNode(this.parentNode);
    }, false, this);