Hai Tien
Hai Tien

Reputation: 3117

JQuery : Remove class, not child content

I use jQuery to append the content into each contentx class like.

  <div id="sidebar">
     <div class="contentx"></div>
     <div class="contentx"></div>
   </div>
  <script>
    $("#sidebar .contentx").each(function()
    {
    //Append here
    }
 </script>

After Append I have, for example :

   <div id="sidebar">
     <div class="contentx">
       something 1 is inserted here.
     </div>
     <div class="contentx">
      something 2 is inserted here.
     </div>
   </div>

but I want to remove class="contentx" whenever the content is appended. This mean I have only :

<div id="sidebar">
 something 1 is inserted here.
 something 2 is inserted here.
</div>

How

Upvotes: 1

Views: 89

Answers (3)

Zahidul Hossein Ripon
Zahidul Hossein Ripon

Reputation: 672

Try this

var tmp = $(".contentx").html();    
$('.contentx').append(tmp);     
var tmp2 = $(".contentx").html();   
$('.contentx').remove();
$('#sidebar').append(tmp2);

Upvotes: 1

Miika L.
Miika L.

Reputation: 3353

Option 1

If you just want to remove the class "contentX" from the div after the content has been added, you can try the following:

$('#sidebar .contextX').each(function () {
    // Append here.
}).removeClass('contextX');

EDIT: Seems I misread the question a little (based on your indicated desired output).

Option 2

If you want to remove the entire element and replace it with the content of your choice? For that, you can try:

$('#sidebar .contextX').each(function () {
    $(this).replaceWith('<new content here.>');
});

jQuery replaceWith

Upvotes: 3

asiviero
asiviero

Reputation: 1235

Besides the append, call removeClass

 $("#sidebar .contentx").each(function()
{
//Append here
    $(this).removeClass('contentx');
}

Upvotes: 1

Related Questions