3
I’m performing an ajax call that retouches the Html filters to be put in a certain div.
However, I would like to remove some elements of html previously. I passed the html to jQuery, thus transforming it into an array.
After removing the elements I would like to resume the concatenated html.
Currently I’m doing so:
msg = jQuery(msg);
var index = 0;
var html = '';
msg.each(function(i, _this){
if(jQuery(_this).find('input[type="submit"]').size() == 1){
index = i;
return;
}
});
msg.splice(index,2);
msg.each(function(){
html += this.outerHTML
});
But I’m not very happy with the development, someone would know how to make that function better?
If you want to remove all elements of type Submit, pq do not select using msg as context and remove all elements returned by select
– Tobias Mesquita
Tobymosque’s answer is a satisfying way to do it, but you may be wondering: what if
msg
not yet in the DOM? Well, as Javascript is synchronous, you can addmsg
to the DOM, to do the operations you want, and even to remove from the DOM afterwards, that all this will happen without any visual artifact on the screen (i.e. the IU will only be updated after your Javascript code returns). Just remember to adapt the selector to take into account the element (maybe temporary) you addedmsg
, if necessary.– mgibsonbr