I have html saved in a variable
var itinerary = $('.events_today').html() ;
I have lots of html and one button I want to remove. It has the id "myButton". How can I remove it from the html saved in my variable
I have html saved in a variable
var itinerary = $('.events_today').html() ;
I have lots of html and one button I want to remove. It has the id "myButton". How can I remove it from the html saved in my variable
Try this:
itinerary.filter(function() { return $(this).not("#myButton"); });
Just locate the element by it's id and remove:
$('#myButton').remove()
Then get your itinerary:
var itinerary = $('.events_today').html() ;
// The above will remove the button from the DOM. To just get the html without having the button taken out of the DOM you can do:
var itinerary = $('.events_today').clone(true).find('#myButton').remove().end().html();
Find the element in the collection and remvoe it.
$('.events_today').find('#myButton').remove();
Edit: After realizing I might've misunderstood the OP, this might work (although not exactly very beautiful).
var html = $('.events_today').html();
// Parse the html, but don't add it to the DOM
var jqHtml = $(html);
// Find and remove the element with the specified id.
jqHtml.find('#myButton').remove();
// Get the new html without the myButton element.
var result = jqHtml.html();
Update: Based on @Fabrizio Calderan's answer you can do what I did above much more nicely with jQuery methods.
Try this:
var html = $('#myHtml').clone(true).find('#elementThatYouWantToRemove').remove().end().html();