datetime_select generates html code that I can't have access

Enrique Barraorion wrote:

<%= datetime_select 'datetime'+@indice.to_s, 'end_date' , :start_year => 2006%>

generates: <select name="datetime0[end_date(1i)]">

And I want to access the value that the user has selected. I have tried with document.form_event.datetime0[end_date(1i)] but javascript gives me an error: "missing ) after argument list" just after the (1i)

I've just been grappling with this. The problem is that JavaScript expects you to use the IDs of elements to reference them, not the names. But when Rails generates these 'select' tags, it only sets the 'name' attribute, and as far as I can see there's no easy way to set the 'id' attribute.

Ideally, if you could generate a select tag that looked like this:

<select name="datetime0[end_date(1i)]" id="datetime0_end_date_1i">

then you could grab it in JavaScript with

document.getElementById('datetime0_end_date_1i");

(Notice that names can contain funny characters like '[', ']', '(' and ')', whereas IDs don't).

Alternatively, it is possible to select an element by its name, but it involves cycling through elements to find it. For example, if you're using the Prototype JavaScript library, you can do something like

var selects = $('my_form').getElementsByTag('select'); for (var i = 0; i < selects.length; i++) {   var select = selects[i];   if (select.name == "datetime0[end_date(1i)]") {     alert(select.value);   } }

assuming that your form has an ID like

<form action="..." id="my_form">

But I'm sure you'll agree that's hardly elegant.

Chris