1

I want to replace some tag-inside-a-paragraph-tag by a heading-tag-enclosed-by-a-paragraph tag. This would result in proper W3C coding, but it seems that jQuery is not able to manipulate the DOM in the right way!? I tried several ways of (jQuery) coding, but i can't get it to work ..

Original code:

<p>some text <span>replace me</span> some more text</p>

Desired code:

<p>some text</p><h2>my heading</h2><p>some more text</p>

Resulting code by jQuery replaceWith():

<p>some text<p></p><h2>my heading</h2><p></p>some more text</p>

Demo: http://jsfiddle.net/foleox/J43rN/4/ In this demo, look at "make H2 custom" : i expect this to work (it's a logical replace statement), but it results in adding two empty p-tags .. The other 2 functions ("make code" and "make H2 pure") are for reference.

Officially the W3C definition states that any heading tag should not be inside a paragraph tag - you can check this by doing a W3C validation. So, why does jQuery add empty paragraph tags? Does anybody know a way to achieve this? Am i mistaken somehow?

Roelof Berkepeis
  • 165
  • 1
  • 12

4 Answers4

1

If you read the jQuery docs, you will find:

When the parameter has a single tag (with optional closing tag or quick-closing) — $("<img />") or $("<img>"), $("<a></a>") or $("<a>") — jQuery creates the element using the native JavaScript createElement() function.

So that is exactly what it is doing. And as I said in my comment, you can't change a parent node from a child node, you're altering the DOM here, not HTML code. So you'll need to either use replaceWith on the parent node and replace everything or use something like remove and append to split it up in multiple elements which you append after each other.

gpgekko
  • 3,506
  • 3
  • 32
  • 35
1

Try this:

var temp = "<p>some text <span>replace me</span> some more text</p>";
temp.replace(/(\<span\>replace me\<\/span\>)/gi, '</p><h2>my heading</h2><p>');

This will do a case insensitive replace for multiple occurences as well.

Read more about capturing groups here

Original credit to this question!

Community
  • 1
  • 1
Vikram Deshmukh
  • 12,304
  • 4
  • 36
  • 38
  • OK, this is the way the replace function works .. it can be useful, but i'm looking for a way to handle DOM elements, not only strings. – Roelof Berkepeis Jan 23 '14 at 20:51
1

You can achieve this with this code. However it's pretty ugly:

$('.replaceMe').each(function() {

    var $parent = $(this).parent(),
        $h2 = $(this).before('$sep$').wrap('<h2>').parent().insertAfter($parent);

    var split = $parent.html().split('$sep$');

    $parent.before('<p>' + split[0] + '</p>');
    $h2.after('<p>' + split[1] + '</p>');

    $parent.remove();
});

http://jsfiddle.net/J43rN/5/

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Your solution works, among other coding answers which don't .. It's a bit ugly indeed, but i guess there are many ways to (quickly) achieve a goal .. I used your code to come up with my definite solution, using your split/after/before idea and not rewriting the parent content, because this (always) generates the unwanted empty p-tags. See http://jsfiddle.net/foleox/J43rN : it holds a general replace function which can also do other replacements alike. – Roelof Berkepeis Jan 23 '14 at 21:16
0

Please try this I have updated the http://jsfiddle.net/J43rN/6/ example by the below java script function please check I hope it will work for you

function fnMakeCode() {
    $('#myP #replaceMe').html("<code id='replaceMe'>My Code</code>");
}
function fnMakeH2pure() {
    $('#myP #replaceMe').html("<h2 id='replaceMe'>My H2 pure</h2>");
}
function fnMakeH2custom() {
     $('#replaceMe').html("<p></p>").html("<h2>My H2 custom</h2>");
}
Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31