After testing your statements, I see you are basically correct.
Contents of all my h2s were replaced by the content of the first one
(no concatenation).  The iteration (using each) and using html()
instead of text() seems to have worked.

Next, I will try the wrapInner technique...

Thank you!

On Aug 5, 11:05 am, mkmanning <michaell...@gmail.com> wrote:
> The solution doesn't work. $("h2").text() concatenates all the text
> from every h2 (it would also miss any markup inside the h2). If you
> want to extract the contents into a variable, then you'll have to
> iterate over the collection of h2's to do the manipulation.
>
> There are other ways to accomplish this in jQuery. Try this:
>
> $("h2").wrapInner(document.createElement('h3')).find('h3').replaceAll
> ('h2');
>
> On Aug 5, 8:33 am, "Cesar Sanz" <the.email.tr...@gmail.com> wrote:
>
> > @ProfCrazyHorse:
> > Your solution seems simple.. why to change something that is working fine?
>
> >   ----- Original Message -----
> >   From: Charlie
> >   To: jquery-en@googlegroups.com
> >   Sent: Wednesday, August 05, 2009 6:38 AM
> >   Subject: [jQuery] Re: replaceWith()
>
> >   there are lots of ways in jQuery to create relationships between 
> > selectors depending on your markup. Without seeing any markup it's hard for 
> > anyone else to help you create those connections
>
> >   ProfCrazyHorse wrote:
> > I want to replace one element with another, and keep the element
> > contents intact.  For example, here, I'm replacing all h2 elements
> > with h3's:
>
> > $("a #replaceWith").click(function () {
> >    var h2Text = $("h2").text();
> >    $("h2").replaceWith("<h3>" + h2Text + "</h3>");
> > });
>
> > I have two questions.
>
> > First, how can I do this more simply?  I tried the following, but $
> > (this) refers to the anchor tag and not the h2 selector.  Is there a
> > way in jQuery to refer to the selector, i.e. $("h2")?
>
> > $("a #replaceWith").click(function () {
> >    $("h2").replaceWith("<h3>" + $(this).text + "</h3>");
> > });
>
> > My second question:  Is there a method more appropriate than
> > replaceWith to replace certain elements on the apge with others?
>
> > Thank you!

Reply via email to