Add Parameters - adammcarth/instance.js GitHub Wiki
Once you've setup your first Instance Model, you can start adding parameters to it. Parameters can be added in three ways:
-
.add( attributes );
- Manually add parameters -
.addField( names );
- Add parameters automatically from html input fields -
.addElement( ids );
- Add parameters automatically from elements in the document
Note: For the purpose of this guide, we'll assume that our instance is in a variable called Comment
:
var Comment = new Instance();
Takes an object with key-value pairs.
Comment.add({
name: "Adam",
handle: "@adammcarth",
subscribe: false,
body: "Hello, world!"
});
Finds html input fields with name="<name>"
, and continuously updates their current values to the instance's parameters.
<input type="text" name="handle" value="@adammcarth">
<textarea name="comment_body">
Hello, world!
</textarea>
// Add a single field (String)
Comment.addField("handle");
// Add multiple fields (Array)
Comment.addField([
"handle",
"comment_body"
]);
Finds elements in the document with id="<id>"
, and continuously adds their innerHtml
values to the instance's parameters.
<div id="price">$12.99</div>
<span id="color">Green!</div>
// Add a single element (String)
Comment.addElement("price");
// Add multiple elements (Array)
Comment.addElement([
"price",
"color"
]);