albuvee
albuvee

Reputation: 2764

jQuery: Remove a closing tag

Is there a way to use jQuery to remove a single closing HTML-Tag?

Example:

  <div class="one">
    <div class="two">CONTENT</div>
  </div>
</div>

I have to remove the last , that means the closing div-tag after div.one is closed.

Unfortunately I have to do this with JS/jQuery since the System I'm working with on this project can't remove it.

Upvotes: 3

Views: 4100

Answers (2)

Dtnand
Dtnand

Reputation: 459

Clone metod:

$tmp = $('.one').clone( true );
$('#wrapper').empty().append($tmp)

Upvotes: 0

Guffa
Guffa

Reputation: 700382

No, that is not possible. There simply are not closing tags at all in the page.

The closing tags exists in the HTML code before it's parsed, once it's parsed each element is one object, not a start and end point in the markup.

The browser will have tried to fix the invalid markup, but the problem is that different browsers will have done different things. Some browsers may ignore the extra closing tag, while others may imply a starting tag to make it a complete element. You could try to rearrange the elements to have them restored to the state they would have been without the extra closing tag, but to do that you need to know what all different browsers will have done when parsing the code.

Even worse, if that code is inside another div, it would be that tag that ended there, and the invalid code would come after that. How that would be handled depends on what the code is outside the code that you have shown, and also there different browsers may handle the invalid code in different ways.

Upvotes: 5

Related Questions