cfreear
cfreear

Reputation: 1875

Missing value attribute when injecting text input using mootools

As noted by this question and in this fiddle, when injecting an input the value attribute doesn't seem to be added to the DOM, MooTools seems to handle it internally. Unfortunately this means that an input injected thus:

new Element("input", {type: "text", value: "injected value"}).inject(parent);

can't be selected like so:

parent.getElements('input[value="injected value"]').length;

(this returns 0 instead of 1 as it would if the value attribute was present)

In my project I need to insert blank input boxes but only if there isn't already a blank one;if(parent.getElements('input[value=""]').length == 0)however this is always resolving to true no matter how many blank inputs there are.

Is there a better way to achieve my goal or is there a fix for what seems like a bug?

Upvotes: 1

Views: 357

Answers (1)

user1726343
user1726343

Reputation:

You could try adding:

var x = new Element("input", {type: "text", value: "injected value"}).inject(parent);
x.setAttribute("value",x.value);

Caveat: While this fixes your issue with the element's value attribute not being reflected, the value attribute is typically not a good way to check if a field is empty, because it is not sync'd with the input's contents.

It would be better to use something like this:

$$('input[type=text]').filter(function(item){return (item.value=="");})

Upvotes: 1

Related Questions