contentand the following JS code: $('myDiv')...">

Mootools replace div content

I have the following HTML code

<div id="myDiv">
   <p>content</p>
</div>

and the following JS code:

$('myDiv').set('html',htmlCode);

The problem is that the htmlCode variable looks something like this:

<div id="myDiv"><p>another content</p></div>

therefore, the result when running the JS code looks something like this:

<div id="myDiv">
   <div id="myDiv">
      <p>another content</p>
   </div>
</div>

Is there a way to use "set" so that it overlaps the entire div? or another solution to get something like:

<div id="myDiv">
   <p>another content</p>
</div>

as a result of a js script? I know that I can just change the htmlCode variable ... I was just wondering if there is another solution for this.

+3
source share
4 answers

Mootools offers a simple replaces method !

//new tmp element that contains the new div
var tmpDiv = new Element('div',{html:'<div id="myDiv"><p>another content</p></div>'});

//new div (first child of my tmp div) replaces the old 'myDiv' (that can be grabbed from the DOM by $)
tmpDiv.getFirst().replaces($('myDiv'));
+6
source
String.implement({
    replaces: function(toReplace) {
        Elements.from(this).inject(toReplace, 'after');
        toReplace.destroy();
    }
});

'<div id="a"><p>ipsum</p></div>'.replaces($('a'));

It has to be done. Example: http://jsfiddle.net/UvuwG/

+5
source
$('myDiv').empty();
$('myDiv').adopt(Elements.from(htmlCode).getElement('p'));
+4

$("myDiv").getParent().set("html", htmlCode);
0

Source: https://habr.com/ru/post/1784623/


All Articles