sawa
sawa

Reputation: 168101

Removing elements from a node

Given a Nokogiri::XML::Node object, how can I remove some of its dom nodes? For example, suppose I have an object whose innerHtml is:

hello world <b>this is in bold face</b> <div>this is inside a div</div> bye bye

How can I remove the <div> element and get a Nokogiri::XML::Node object whose innerHtml is:

hello world <b>this is in bold face</b>  bye bye

Upvotes: 0

Views: 155

Answers (1)

mu is too short
mu is too short

Reputation: 434665

Find the <div> and call remove on it. For example:

>> node.to_html
=> "<div>hello world <b>this is in bold face</b> <div>this is inside a div</div> bye bye</div>"
>> node.at('div').remove; node.to_html
=> "<div>hello world <b>this is in bold face</b>  bye bye</div>"

Upvotes: 1

Related Questions