# Widget-like API for DOM objects?

**URL:** <https://www.ruby-forum.com/t/widget-like-api-for-dom-objects/52395>\
**Category:** Ruby\
**Created:** [January 13, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/widget-like-api-for-dom-objects/52395 "2006-01-13T16:42:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 13, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/widget-like-api-for-dom-objects/52395/1 "2006-01-13T16:42:56Z")

</div>

Hi all,

Is there something out there that would let me create DOM objects in a  
more  
Rubyesque manner? For example, say I want to create a ‘select’ object  
in a web  
page. I’d like an API something along these lines:

select = Select.new  
select.options = [“joe”, “bob”, “moe”] # or {1=\>“joe”, 2=\>“bob”,  
3=\>“moe”}  
select.selected\_index = 2  
select.title = “Three guys”  
select.access\_key = “A”

p select.html # Show html that would be generated using above options

You get the picture. Is there anything like this out on the RAA?  
Nothing  
immediately jumped out at me.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 13, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/widget-like-api-for-dom-objects/52395/2 "2006-01-13T17:19:24Z")

</div>

Dan

I can give you this: [http://scm.ywesee.com/?p=htmlgrid](http://scm.ywesee.com/?p=htmlgrid)

However, this was one of my very first ruby-projects and basically needs  
a complete rewrite… Some of the reasons:

- Geared towards Table-Layout, hence the ‘grid’
- tight coupling to [http://scm.ywesee.com/?p=sbsm](http://scm.ywesee.com/?p=sbsm) which is a  
state-based session-manager
- meta-programming by configuration-constants
- naming conventions changed in mid-project
- nonexistant documentation

That said, if you want to try it out and help me get the priorities for  
a next version straight, you’re more than welcome to do so and I hereby  
pledge my support - which you’ll probably need 😉

Cheers  
Hannes

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 13, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/widget-like-api-for-dom-objects/52395/3 "2006-01-13T17:40:39Z")

</div>

Hannes W. wrote:

> List-Unsubscribe: [mailto:removed\_email\_address@domain.invalid?body=unsubscribe](mailto:removed_email_address@domain.invalid?body=unsubscribe)
> 
> - meta-programming by configuration-constants
> - naming conventions changed in mid-project
> - nonexistant documentation
> 
> That said, if you want to try it out and help me get the priorities for  
> a next version straight, you’re more than welcome to do so and I hereby  
> pledge my support - which you’ll probably need 😉

I tried to look at the link you provided but I get a 403 forbidden. ☹

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 13, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/widget-like-api-for-dom-objects/52395/4 "2006-01-13T17:46:42Z")

</div>

On 1/13/06, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tried to look at the link you provided but I get a 403 forbidden. ☹

sorry 'bout that, must have been a copy-paste thingummy… Try again:  
[http://scm.ywesee.com/?p=htmlgrid](http://scm.ywesee.com/?p=htmlgrid)

Hannes

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 13, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/widget-like-api-for-dom-objects/52395/5 "2006-01-13T17:58:49Z")

</div>

On 1/13/06, Hannes W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 1/13/06, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I tried to look at the link you provided but I get a 403 forbidden. ☹

I must learn to think before posting… I’ve made a tarball, just in  
case you don’t want to install git to download the code:  
[http://download.ywesee.com/htmlgrid/htmlgrid-daily.tar.bz2](http://download.ywesee.com/htmlgrid/htmlgrid-daily.tar.bz2)  
[http://download.ywesee.com/sbsm/sbsm-daily.tar.bz2](http://download.ywesee.com/sbsm/sbsm-daily.tar.bz2)

Apologies for all the noise…  
Hannes

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 13, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/widget-like-api-for-dom-objects/52395/6 "2006-01-13T20:52:56Z")

</div>

- Daniel B. (removed\_email\_address@domain.invalid) wrote:

> select.access\_key = “A”
> 
> p select.html # Show html that would be generated using above options
> 
> You get the picture. Is there anything like this out on the RAA? Nothing  
> immediately jumped out at me.

REXML has a DOM-like API, and it comes pre-installed with Ruby:

# load REXML

require ‘rexml/document’

# 

# Create an HTML select element.

# 

class HTMLSelect  
attr\_accessor :options, :attrs, :index

```
def initialize(options = [], select_attrs = {}, selected_index = 

```

nil)  
@options, @attrs, @index = options, select\_attrs, selected\_index  
end

```
def to_s
  # create return select element and append requested attributes
  ret = REXML::Element.new('select')
  @attrs.each { |key, val| ret.attributes[key] = val }

  # iterate over, create, and append each option
  @options.each_with_index do |opt, i|
    # create and append element
    opt_elem = ret << REXML::Element.new('option')

    # add option text, and option attributes
    opt_elem << REXML::Text.new(opt)
    opt_elem.attributes['value'] = i.to_s
    opt_elem.attributes['selected'] = 'Y' if i == @index
  end

  # convert select element to string and return
  ret.to_s
end

```

end

# define attributes, option items, and initial selected index

select\_attrs = {  
‘name’ =\> ‘which\_guy’,  
‘title’ =\> ‘Three Guys’,  
‘access\_key’ =\> ‘A’,  
}  
selected\_index, options = 2, %w{joe bob moe}

# build select box and print out result

puts HTMLSelect.new(options, select\_attrs, selected\_index).to\_s

Hope this helps…

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 13, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/widget-like-api-for-dom-objects/52395/7 "2006-01-13T20:55:58Z")

</div>

Paul D. wrote:

> > select.selected\_index = 2
> 
> ```
> @options, @attrs, @index = options, select_attrs, selected_index
> opt_elem = ret << REXML::Element.new('option')
> 
> ```
> 
> end  
> puts HTMLSelect.new(options, select\_attrs, selected\_index).to\_s
> 
> Hope this helps…

Ah, thanks Paul. I’d like something prebuilt, but it looks like REXML  
would  
serve as a great way to build things behind the scenes.

Regards,

Dan
