Tom
Tom

Reputation: 6707

how to replace both the content of DIV and class name

I want to locate a div and replace both the content and div class name with another, how to do that

<div class="replaceme1"> 
  replace me 2, too

Upvotes: 2

Views: 271

Answers (4)

user669677
user669677

Reputation:

If you want to replace everything:

$('#replace_this').after('#to_this').remove();

Upvotes: 0

Soravux
Soravux

Reputation: 9983

Assuming you use the jQuery library, check out:

  1. The .text() or .html() attribute to modify it's content;
  2. The .addClass() and .removeClass() attribute; or
  3. The .attr() attribute;

to modify it's class. Note that the latter will require you to use quotes.

So, for your example, you would do:

$('div.replaceme1')
    .removeClass('BottomSmMargin MiniCheckDiv')
    .text('Hello world!');

Upvotes: 1

Evil Andy
Evil Andy

Reputation: 1748

$('div.replaceme1')
    .removeClass('replaceme1')
    .addClass('Foo')
    .html('<p>Some new text</p>')

Have a read of .html(), .removeClass() and .addClass()

Upvotes: 0

Alex
Alex

Reputation: 14648

$("div.replaceme1")
    .html("<p>new text</p>")
    .removeClass("replaceme1")
    .addClass("SomeNewClassReplacement");

Upvotes: 9

Related Questions