Getting the listbox object

J

joe

I have a listbox:

<select name="drop1" id="test" onclick="showme()">
</select>

which I populate onload. When user clicks an item in the listbox function
showme() fires up. How do I get the listbox object and which item (index) was
clicked? I don't want to use getElementsById.

function showme()
{
var i;
i=this; //this won't work
...
}

I looked up google but just could not find a solution. The question may be to
elementary! I am learning JavaScript!
 
E

Elegie

On 25/06/2011 12:01, joe wrote :

Hi,
I have a listbox:

<select name="drop1" id="test" onclick="showme()">
</select>

which I populate onload. When user clicks an item in the listbox function
showme() fires up. How do I get the listbox object and which item (index) was
clicked? I don't want to use getElementsById.

When you define an event listener through an HTML attribute, the browser
gets a reference to the DOM element, then creates an anonymous listener
function, sets its body to the value of the attribute, and assigns it to
the element, equivalent in your example to the following:

---
selectElement.onclick = (function () {
showme();
});
---

In most browsers:
- the anonymous listener, when called, is passed the event object for
which it is registered,
- the "this" value, in the listener body, refers to the element for
which the event is fired.

Therefore, you could have...

---
<select name="drop1" onclick="showMe(this, arguments[0]);">
---

which would translate as...

---
selectElement.onclick = (function () {
showMe(this, arguments[0]);
});
---

with the following showMe function...

---
function showMe(eventTarget, eventObject) {
// do stuff
}
---

The showMe function could also omit the eventObject argument, if you
have no use for it.

To conclude, two additional remarks:
- You use an onclick event attribute. What happens if your user
navigates the form using only the keyboard, not his mouse?
- To know more about form elements and their values, check the following
FAQ entry:

<URL:http://www.jibbering.com/faq/#formControlAccess>

HTH,
Elegie.
 
D

Denis McMahon

I have a listbox:

<select name="drop1" id="test" onclick="showme()"> </select>

which I populate onload. When user clicks an item in the listbox
function showme() fires up. How do I get the listbox object and which
item (index) was clicked? I don't want to use getElementsById.

function showme()
{
var i;
i=this; //this won't work
..
}

I looked up google but just could not find a solution. The question may
be to elementary! I am learning JavaScript!

1) Why don't you want to use document.getElementById?

2) Why use the onclick handler? This will trigger when someone clicks on
the select field, possibly before they have chosen their option. It might
be better to use "onchange" which will trigger after they change the
field, or even onblur, which will trigger when they leave the field,
whether they changed it or not.

User clicks on a field. onclick event triggers.
User clicks on or tabs to a field. onfocus event triggers.
User tabs or clicks away from a field. onblur event triggers.
User clicks on or tabs to a field, changes the field, clicks or tabs away
from the field. onchange and onblur events occur.

3) I think you can use this in the event attribute of the html tag. Try
the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=us-ascii">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>event tests</title>
<script type='text/javascript'>
function clicked(obj) {sendOutput('Clicked: ' +
obj.name + '/' + obj.id);}
function changed(obj) {sendOutput('Changed: ' +
obj.name + '/' + obj.id);}
function focused(obj) {sendOutput('Focused: ' +
obj.name + '/' + obj.id);}
function blurred(obj) {sendOutput('Blurred: ' +
obj.name + '/' + obj.id);}
function sendOutput(msg) {
var theField = document.getElementById('output1');
var txt = theField.value.split('\n');
var l = txt.push(msg);
if (l > theField.rows) txt.shift();
theField.value = txt.join('\n');
}
function init() {document.getElementById('output1').value='';}
</script>
</head>

<body onload='init()'>

<form action='events.htm' method='get'>

<p>
Text:
<input type='text' name='field1' id='field1'
onclick='clicked(this)' onfocus='focused(this)'
onchange='changed(this)' onblur='blurred(this)'>
Select:
<select name='field2' id='field2'
onclick='clicked(this)' onfocus='focused(this)'
onchange='changed(this)' onblur='blurred(this)'>
<option value='0'>Choose ...</option>
<option value='1'>Andrew</option>
<option value='2'>Brian</option>
<option value='3'>Colin</option>
<option value='4'>David</option>
<option value='5'>Edward</option>
</select>
Checkbox:
<input type='checkbox' name='field3' id='field3'
onclick='clicked(this)' onfocus='focused(this)'
onchange='changed(this)' onblur='blurred(this)'>
Radio: [
1:
<input type='radio' name='field4' id='field4[0]' value='0'
onclick='clicked(this)' onfocus='focused(this)'
onchange='changed(this)' onblur='blurred(this)'>
2:
<input type='radio' name='field4' id='field4[1]' value='1'
onclick='clicked(this)' onfocus='focused(this)'
onchange='changed(this)' onblur='blurred(this)'>
3:
<input type='radio' name='field4' id='field4[2]' value='2'
onclick='clicked(this)' onfocus='focused(this)'
onchange='changed(this)' onblur='blurred(this)'>
4:
<input type='radio' name='field4' id='field4[3]' value='3'
onclick='clicked(this)' onfocus='focused(this)'
onchange='changed(this)' onblur='blurred(this)'> ]
<br>
<textarea cols='30' rows='30' name='output1' id='output1'>
</textarea>

</p>

</form>

</body>
</html>

As you navigate between (using mouse or k/b) and change the form fields,
it will display the sequences of events that are triggered in the
textarea. This might help you determine which events are best to use for
your purpose.

Rgds

Denis McMahon
 
D

dhtml

On 25/06/2011 12:01, joe wrote :

Hi,
I have a listbox:
<select name="drop1" id="test" onclick="showme()">
</select>
which I populate onload. When user clicks an item in the listbox function
showme() fires up. How do I get the listbox object and which item (index) was
clicked? I don't want to use getElementsById.

When you define an event listener through an HTML attribute, the browser
gets a reference to the DOM element, then creates an anonymous listener
function, sets its body to the value of the attribute, and assigns it to
the element, equivalent in your example to the following:

---
selectElement.onclick = (function () {
   showme();});

---

In most browsers:
- the anonymous listener, when called, is passed the event object for
which it is registered,
- the "this" value, in the listener body, refers to the element for
which the event is fired.

Therefore, you could have...

---
<select name="drop1" onclick="showMe(this, arguments[0]);">
---

which would translate as...

---
selectElement.onclick = (function () {
   showMe(this, arguments[0]);});

---

with the following showMe function...

---
function showMe(eventTarget, eventObject) {
   // do stuff}

---

The showMe function could also omit the eventObject argument, if you
have no use for it.

To conclude, two additional remarks:
- You use an onclick event attribute. What happens if your user
navigates the form using only the keyboard, not his mouse?
Onclick will fire. mouseevents just work. There was a W3C proposal for
activation events but it wasn't widely adopted and since mouseevents
work, people kept using them.

There is now a proposal for a feature that is both new and deprecated.
http://www.w3.org/TR/DOM-Level-3-Events/#event-type-DOMActivate

They're still working on it...
- To know more about form elements and their values, check the following
FAQ entry:

<URL:http://www.jibbering.com/faq/#formControlAccess>

HTH,
The FAQ could use such help.
 
E

Elegie

On 30/06/2011 06:32, dhtml wrote :

Hello Garrett,
Onclick will fire. mouseevents just work. There was a W3C proposal for
activation events but it wasn't widely adopted and since mouseevents
work, people kept using them.

There is now a proposal for a feature that is both new and deprecated.
http://www.w3.org/TR/DOM-Level-3-Events/#event-type-DOMActivate

I have never used activation events nor DOMActivate events so far;
thanks for the link!

This makes me a bit nostalgic. Somehow, I am glad to see that the
capabilities of modern browsers are continuously being expanded, not
only from a scripting point of view, but also with other technologies,
such as HTML5 (I have recently discovered the canvas - just love it!).

Has the building of rich client interfaces finally become viable now? I
remember many considerable issues one had to solve when scripting robust
behaviors on the client: graceful degradation (what happens if no
javascript is available, if the platform has javascript but does not
support the feature, or supports it in a funny way...), security (the
user changing values of restricted form controls through personal
scripting...).
The FAQ could use such help.

Thank you for your appreciation! If you happen to like some code or
explanations I have written on c.l.j, and want to reuse it for the FAQ
(or for anything else), then you are of course welcome to do it.

Also, I have noticed that the FAQ currently lacks a maintainer. I do not
want to misinterpret, but if you would like to suggest I could consider
and apply for the position, then I am afraid I would have to decline. I
am not a regular in the group (which is the most important quality for a
FAQ maintainer), and while my growing-old javascript knowledge could
probably be beefed up, I do not intend to stay here for more than a few
weeks (I am on a professional break for now, but this shall not last).

Kind regards,
Elegie.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
(I have recently discovered the canvas - just love it!)

You might like to look at the following canvas-using pages :
1 <http://www.merlyn.demon.co.uk/astron-5.htm>
2 <http://www.merlyn.demon.co.uk/gravity5.htm>
3 <http://www.merlyn.demon.co.uk/js-grphx.htm>
4 <http://www.merlyn.demon.co.uk/js-randm.htm>
5 <http://www.merlyn.demon.co.uk/sitedata.htm>
6 <http://www.merlyn.demon.co.uk/uksumtim.htm>

In those :
1 has drag'n'drop graphics, linear and rotational;
2 & 4 have moving graphics;
3 can run user graphic code;
6 has crude pan & zoom (in X).
 
E

Elegie

On 01/07/2011 22:54, Dr J R Stockton wrote :

Hello John,

Thank you for those nice examples! When I first encountered the canvas,
I thought at first about using it for common business reporting (nothing
fancy), but it is very interesting to see other possible usages.

Cheers,
Elegie.
 

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
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top