0

I have a HTML file in the below format :-

<div class="container">
<div class="hello"><p>1</p></div>
<div class="goodbye">2</div>
<div class="hello"><p>3</p></div>
<div class="goodbye">4</div>
</div>

Please recommend me a program which could remove a particular div tag by its class name and save the output file as below :-

<div class="container">
<div class="goodbye">2</div>
<div class="goodbye">4</div>
</div>

The whole division along with its internal tags should be removed. I have used jQuery, but it does not affect the source code.
Thanks in advance.

Volker E.
  • 5,911
  • 11
  • 47
  • 64
vrtech77
  • 149
  • 3
  • 14

3 Answers3

2

You can use .remove():

Remove the set of matched elements from the DOM.

$('.container .hello').remove();

Side note: You can use .find() to speed up above selector:

$('.container').find('.hello').remove();
Felix
  • 37,892
  • 8
  • 43
  • 55
  • It only affects what I see in the browser. The HTML source is never changed. I want the HTML source to change so that I could copy and save the new HTML code. – vrtech77 May 09 '14 at 10:41
1

You can get the element having class hello within container and call .remove()

Live Demo

$('.container .hello').remove();

Similar to .empty(), the .remove() method takes elements out of the DOM. Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .detach(), jQuery docs

Adil
  • 146,340
  • 25
  • 209
  • 204
  • It only affects what I see in the browser. The HTML source is never changed. I want the HTML source to change so that I could copy and save the new HTML code. – vrtech77 May 09 '14 at 10:41
  • @Adil he wants the element should be removed in html page source it is not possible – Sudharsan S May 09 '14 at 10:45
  • If you want what @sudharsan has told then it is not possible and I cant understand why you want this. – Adil May 09 '14 at 10:53
  • There are some things inside a scraped data which I do not want. I wan to remove particular divisions from them by their class name from the HTML source code. – vrtech77 May 09 '14 at 11:04
  • You can not delete from source file through script. although you can put conditions in server side code to generate html in the way you want – Adil May 09 '14 at 11:05
0

So, nobody actually seemed to read what OP asked for.
Here's an answer for a JavaScript Regular Expression, very dirty and unflexible, but matching your needs.

<div class=.(\w*)?.><(.*)</div>

Still you may run into problems, because I don't know any editor actually using JavaScript RegEx.

Basically, everything about problems you might run into has been already said in this famous thread: RegEx match open tags except XHTML self-contained tags

Volker E.
  • 5,911
  • 11
  • 47
  • 64