0

I am trying to replace some HTML Tag with another HTML Tag with php or jquery .

My Current Tag (Default) :

<li class="dropdown"> <a href="index.html" class="dropdown-toggle"> Home</a></li>

I want to replace above HTML tag with

<li class="dropdown"> <a href="index.html" class="dropdown-toggle active"> Home</a></li>

Example :

Default tag : <h1> tag </h2>

Replaced tag : <h2> tag </h2>

I already search many articles on stackoverflow but didnt founded solution for it.

Thank in advance

3 Answers3

1

Update

Just saw your question again. You just need to add:

$(".dropdown-toggle").addClass("active");

Example:

Default tag: <h1> tag </h2>
Replaced tag: <h2> tag </h2>

You can use .replaceWith():

$("h1").replaceWith(function() {
  return $("<h2 />", {
    html: $(this).html()
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1> tag </h1>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • @RajeevRanjanSharma Check again... – Praveen Kumar Purushothaman Jan 13 '16 at 20:51
  • Like this `$("
  • ").replaceWith(function() { return $("", { html: $(this).html() }); });` – Rajeev Ranjan Sharma Jan 13 '16 at 20:56
  • @RajeevRanjanSharma Man, I have updated again. See the update. Refresh the answer. – Praveen Kumar Purushothaman Jan 13 '16 at 20:56
  • @RajeevRanjanSharma You just need `$(".dropdown-toggle").addClass("active");`. That's it. – Praveen Kumar Purushothaman Jan 13 '16 at 20:56