determining name of form element

B

Bosconian

I've hunted high and low, but I can't figure out how to retrieve the name of
an element when onclick occurs.

<select name="myselect" onclick="alert(this.form.elements.name)">

returns a blank alert box.

Can someone clue me in? Thanks.
 
M

Matt Kruse

Bosconian said:
I've hunted high and low, but I can't figure out how to retrieve the
name of an element when onclick occurs.

You didn't try the obvious "name" property?
<select name="myselect" onclick="alert(this.form.elements.name)">

First, the "onclick" event doesn't apply to select elements.

But from within a form element's event handler you can always reference
this.name to get the control's name.
 
R

Randy Webb

Bosconian said the following on 5/2/2006 11:21 PM:
I've hunted high and low, but I can't figure out how to retrieve the name of
an element when onclick occurs.

onclick="alert(this.name)"
 
R

Randy Webb

Matt Kruse said the following on 5/2/2006 11:52 PM:
You didn't try the obvious "name" property?


First, the "onclick" event doesn't apply to select elements.

Are you sure? I won't say all browsers, but I can say that IE6, Firefox,
Opera7/8/9 and Mozilla proper on WinXP SP2 all fire the onclick of a
Select element:

<select name="mySelect" onclick="alert(this.name)">

Not sure what good it is but it fires.
But from within a form element's event handler you can always reference
this.name to get the control's name.

If it has a name attribute :)
 
B

Bosconian

First, the "onclick" event doesn't apply to select elements.

While "onclick" does in fact work perfectly fine with select inputs, I ended
up switching to "onfocus" for keyboard compatibility.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top