marius
marius

Reputation: 1266

Mootools new Element does not work

new Element does not work. Someone has any clue why? css:

#tileNode {
position:absolute;
width:100px;
height:100px;
background-color:red;
}

js:

window.addEvent('domready', function() {
var newElement = new Element('div',{id:"tileNode"});
newElement.inject($$('div#tileNode'));
alert($$('#tileNode').get('id'));
});

Alerting #root works fine so it exists... creating that tileNode element does not work alerting it also does not work :/

Kind regards!

Upvotes: 0

Views: 99

Answers (3)

lino
lino

Reputation: 85

There are few things in your code that took my attention: Fist is, new Element('div',{id:"tileNode"}) is completely equal to new Element('div[id=tileNode]'). Since it makes no difference what way you choose, I'd recommend the second form, because it is a lot more readable. You could even add more attributes like Element('div[id=tileNode][class=tile]') and so on, instead of passing an object.

Another optimization would be to use the dollar function if you are trying to get an element by its id. So if you want to alert the id of an element, try to use alert($('tileNode').id). Though alerting might work fine, you could try to use the javascript console. Instead of alert(myVar) use console.log(myVar).

Hope this helps, and happy coding :)

Upvotes: 1

hamburger
hamburger

Reputation: 1435

you can also use this to inject:

newElement.inject($$('div#tileNode')[0]);

Upvotes: -1

Romain
Romain

Reputation: 810

use

newElement.inject($('root'));

If you inject in the div with the id 'root',also you have to inject in a single element, use $('id') because the $$ returns an array…

Upvotes: 1

Related Questions