0

I am facing a problem where $('element').html('<p>Some HTML</p>'); is not working properly. After executing $('element').remove(); function the $('element').html(); is working fine. Example Code below:

<div id="someId"><p>Html before</p></div>
//html() Not working
$('#someId').html("<h1>Some changed Html</h1>");


$('#someId').remove();
//After remove() the html() also works
$('#someId').html("<h1>Some changed Html</h1>");
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 4
    I would assume you have multiple elements in the DOM with the same `#someId` value, which is invalid. When you call `remove()` the first one is removed. When you then call `html()` again, the element you expect to update is selected. To check this, do a search in the DOM inspector for the id you're trying to find. If there is more than one, that's the problem. – Rory McCrossan May 20 '21 at 16:10
  • 1
    In general, IDs should be unique. And jQuery assumes that this is true, so ID selectors will only select the first match. – Barmar May 20 '21 at 16:17
  • 1
    Thanks a lot @RoryMcCrossan. You are right and the DOM consits multiple elements with same ID. It's fine now. – Tahir Aziz May 21 '21 at 10:11
  • No problem, glad you got it working – Rory McCrossan May 21 '21 at 10:12

0 Answers0