Datetime_select generates html code that I can't have access

Hello:

<%= datetime_select 'datetime’[email protected]_s, ‘end_date’ , :start_year =>
2006%>

generates:

2006 2007 2008 2009 2010 2011 January February March ....

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)
any idea
thanks in advance

Enrique Barraorion wrote:

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:

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

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

Chris

2010 document.form_event.datetime0[end_date(1i)] but javascript gives me an error: "missing ) after argument list" just after the (1i) any idea thanks in advance

I think something like this would work…

document.form_event.elements[“datetime0[end_date(1i)]”].xxxxxxx

-philip

Philip H. wrote:

2010 document.form_event.datetime0[end_date(1i)] but javascript gives me an error: "missing ) after argument list" just after the (1i) any idea thanks in advance

I think something like this would work…

document.form_event.elements[“datetime0[end_date(1i)]”].xxxxxxx

-philip

Thank you very much. That worked very well.
I would have never realized about it
thanks
thanks
thanks

Philip H. wrote:

document.form_event.elements[“datetime0[end_date(1i)]”].xxxxxxx

Holy crap, that’s fantastic. How have I managed to not notice this
before?

Thanks!

Chris