Aug
30
2007

Learning Dojo 0.9 : Hello TextBox

The goal of this exercise is to add interactivity between two dijits, TextBox and Button.

add TextBox

load TextBox dijit

Add another dojo.require after the former two.

dojo.require("dijit.form.TextBox");

add TextBox dijit

Add the following code before button dijit.


<input id="input" class="medium" name="string" type="text" />
<button id="hello" onclick="log()">Hello Dijit</button>
Press to output in console.

extra attributes

Notice when adding TextBox, a few extra attributes are specified

  • trim, tells dojo to remove leading and tailing spaces in the input
  • propercase, tells dojo to capitalize the first letter of each word separated by spaces

augment Button onClick function

In this exercise, when the button is pressed, we want the function to read values in the TextBox first, then output the msg to the console.

dijit.registry

To find the TextBox dijit value, we need to find the TextBox dijit first.
dijit.registry, by its name we can guess, contains information about all the dijits in this page. Therefore we can utilize it to find the TextBox dijit and its value.

get the value


//Find the dijit by its id;
var input = dijit.registry.byId("input");
//Get the value of the TextBox by calling its function getValue
var msg = input.getValue();

wrapping up as a function

Replace var log = console.debug in last example with the following code


var log = function(){
var input = dijit.registry.byId("input");
var msg = input.getValue();
if (msg == ""){
msg = "nothing";
}
console.debug("I was pressed to say " + msg);
}

Finally, modify the onClick attribute of the Button.

<button id="hello" onclick="log()">Hello Dijit</button>

Screenshot

Example

Example: lab004.html
Source Code: lab004.txt

Tags: , , , ,
» Posted in category: ajax, dojo, lab, web development //
Entry Top //
This is the bottom of post Learning Dojo 0.9 : Hello TextBox

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>