Checkbox Implementation

188 views
Skip to first unread message

siddharth trikha

unread,
May 17, 2012, 3:11:19 AM5/17/12
to Closure Library Discuss
How to create a checkbox dynamically i.e without writing any html
element in .html file?
I do not want to use createDom like this:

var checkBox = goog.dom.createDom('input', {'type': 'checkbox', 'id':
storyId + '=checkbox'});


Is there a way? Help!

ramesh

unread,
May 17, 2012, 10:45:00 AM5/17/12
to closure-lib...@googlegroups.com

Rhys Brett-Bowen

unread,
May 17, 2012, 5:16:21 PM5/17/12
to closure-lib...@googlegroups.com
you could use:

var checkBox = goog.dom.htmlToDocumentFragment('<input type="checkbox" id="' + storyId + '=checkbox">');

Why don't you want to use goog.dom.createDom? What would you like to do?

Chris Wilson

unread,
May 17, 2012, 5:11:20 AM5/17/12
to Closure Library Discuss
Hi Siddharth,
Look at the Checkbox demo, especially the render() method, to see how it
uses the checkbox object's render() method to dynamically create the DOM
(which does for you more or less what you do above).

You may not easily be able to control the ID of the component without
creating the DOM element yourself.

Cheers, Chris.
--
_____ __ _
\ __/ / ,__(_)_ | Chris Wilson <chri...@qwirx.com> Cambs UK |
/ (_/ ,\/ _/ /_ \ | Security/C/C++/Java/Ruby/Perl/SQL Developer |
\__/_/_/_//_/___/ | We are GNU : free your mind & your software |

siddharth trikha

unread,
May 18, 2012, 7:46:45 AM5/18/12
to Closure Library Discuss
I want to use Closure Library widgets as much as possible, not
createDom.

Rhys Brett-Bowen

unread,
May 19, 2012, 10:04:42 AM5/19/12
to Closure Library Discuss

You could extend goog.ui.checkbox and add in a createDom method

On May 18, 4:46 am, siddharth trikha <siddharthtrik...@gmail.com>
wrote:

siddharth trikha

unread,
May 23, 2012, 4:36:15 AM5/23/12
to Closure Library Discuss
Can you please explain how to do it or give a link / reference ?

On May 18, 4:46 pm, siddharth trikha <siddharthtrik...@gmail.com>
wrote:

Rhys Brett-Bowen

unread,
May 24, 2012, 2:24:16 PM5/24/12
to Closure Library Discuss
had a look and there is already a checkBoxRenderer. Try this:

go to: http://closure-library.googlecode.com/svn/trunk/closure/goog/demos/checkbox.html

open up your javascript console and type these in:

a = new goog.ui.Checkbox;
a.render(document.body);

that's it! the goog.ui.Checkbox has a default renderer that will
create the dom for you (render will run the createDom method which
will run the createDom method of the renderer which by default is
goog.ui.CheckboxRenderer). the render will then append the element to
whatever you pass in - in this case I just added it to the body so you
should see a new checkbox at the bottom.

You can change how the Dom is created by passing in a different
renderer - take a look at http://closure-library.googlecode.com/svn/docs/class_goog_ui_CheckboxRenderer.html
to see the default and how that is made).

On May 23, 1:36 am, siddharth trikha <siddharthtrik...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages