4

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

2
  • That's a tough one. I'd like to see the best way to do this. Any chance you can't remove it after you append it? Commented Feb 7, 2013 at 16:56
  • @Roscoeh, have you tried some of the proposed solutions? Commented Mar 19, 2014 at 8:43

5 Answers 5

10

I suggest this approach

var itinerary = $('.events_today')
                .clone(true)       /* get a clone of the element and from it */
                .find('#myButton') /* retrieve the button, then */
                .remove()          /* remove it, */
                .end()             /* return to the previous selection and */
                .html() ;          /* get the html */
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

itinerary.filter(function() { return $(this).not("#myButton"); });

1 Comment

this helped me for removing element from ajax response, eg: $(response).not('#element_id');
0

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();

1 Comment

I don't think he wants to remove it from the DOM, just the variable
0

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.

1 Comment

See my comment on qci's answer
0

Try this:

var html = $('#myHtml').clone(true).find('#elementThatYouWantToRemove').remove().end().html();

1 Comment

Why are you cloning the parent element?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.