Form.Element.setValue()

by @jehiah on 2005-12-20 16:35UTC
Filed under: All , Javascript , ajax

You love prototype.js, and you use Form.Element.getValue() all the time. Here is the Form.Element.setValue() counterpart. The setValue() function can be found in my prototypeUtils.js

It works like this

[js]
Form.Element.setValue("username","Jehiah");
Form.Element.setValue("usertype","admin");
[/js]

No need to worry if it’s a textarea, text field, select box, check box, or a radio box. It just works.

This also allows for some beautifull JSON integration. Say you want to load a form with values that you get back from an ajax call (the Ajax part is an exercise left for you - the reader)

[js]
var userdata = {"username":"jehiah","usertype":"admin"};
[/js]

We can write a simple function to unpack those values into the form

[js]
function unpackToForm(data){
   for (i in data){
     Form.Element.setValue(i,data[i].toString());
   }
}
[/js]

Now to unpack that data to our form we just use

[js]
unpackToForm(userdata);
[/js]

Nice =)

Subscribe via RSS ı Email
Jehiah Czebotar