1

How to create DOM elements from string (pass from ajax) in Mootools?

In jQuery a simple solution is $( elements )

var elements = '<i>This is italic</i><b>this bold</b>...';
Tiago Sippert
  • 1,324
  • 7
  • 24
  • 33
Bogdan
  • 171
  • 3
  • 13

3 Answers3

6

Simple as: Elements.from('<i>This is italic</i><b>this bold</b>')

Oskar Krawczyk
  • 3,492
  • 17
  • 20
1

Without a string, you would use the Element class:

var el = new Element('div#id.class', {
    text: 'My text',
});

With a string, you can check how it's one in Request.HTML, see here.

var temp = new Element('div').set('html', response.html);
response.tree = temp.childNodes;
response.elements = temp.getElements(options.filter || '*');

Basically Mootools elements & DOM elements are the same, this is another SO questions which creates DOM nodes from HTML: Creating a new DOM element from an HTML string using built-in DOM methods or prototype

From old Mootools forums, I found an interesting idea too: add a new method Element.fromString() or String.toElement() which would contain this logic.

Community
  • 1
  • 1
Savageman
  • 9,257
  • 6
  • 40
  • 50
0

I'm using the latest Mootools 1.6.0.
It throws Elements.from is not a function.

This one works for me:

var html = '<img src='+item.src+'>';
var el = new Element('li').set('html', html);

The working code: http://jsfiddle.net/chetabahana/qbx9b5pm/

eQ19
  • 9,880
  • 3
  • 65
  • 77