giving buttons the same onclick event

D

drawde83

Hi,
I'm mocking up an interface in javascript for an HCI assignment. I want
to be able to make the default onclick event for the buttons on my page
to display an alert since the buttons won't work yet. Is there an easy
way to do this? and it has to be as easy as giving each button their
own onclick attribute otherwise I'll just do that.
 
L

Lee

Dr Clue said:
Hi,
I'm mocking up an interface in javascript for an HCI assignment. I want
to be able to make the default onclick event for the buttons on my page
to display an alert since the buttons won't work yet. Is there an easy
way to do this? and it has to be as easy as giving each button their
own onclick attribute otherwise I'll just do that.

Well, making it the default behavior is not too hard.


<html><head>
<script type="text/javascript" language="javascript1.2">
<!--
function myClick(oI)
{
alert("Under Construction " +oI.name)
}
function captureInputs()
{
aINPUTcollection=self.document.getElementsByTagName("input")
for(x=0;x<aINPUTcollection.length;x++)
{
oI=aINPUTcollection[x]
if(oI.type="button")if(!oI.onclick);
oI.onclick=new Function("return myClick(this)")
}
}
// -->
</script>
<body onload="captureInputs()">
<form name="foo">
<input type="button" name="foo1" value="foo1">
<input type="button" name="foo2" value="foo2">
<input type="button" name="foo3" value="foo2"
onclick="return false">
</form>
</body>
</html>

1. Please don't encourage people to use the <-- //--> comments in script.
2. Wouldn't it be better to use a decent text editor and replace each occurance
of /<input type="button"/ with /<input type="button" onclick="notyet()"/ ?
 
R

Richard Cornford

Dr Clue wrote:
<html><head>
<script type="text/javascript" language="javascript1.2">

Encouraging browsers that will to use the divergent type-converting and
assignment behaviour unique to JavaScript 1.2 is a very bad idea.

The 'hide from older browsers' HTML-style comments are now redundant,
and a bad habit to encourage in others who may eventually be asked to
script XHTML, where they are potentially fatal.
function myClick(oI)
{
alert("Under Construction " +oI.name)
}
function captureInputs()
{
aINPUTcollection=self.document.getElementsByTagName("input")
for(x=0;x<aINPUTcollection.length;x++)
{
oI=aINPUTcollection[x]

There does no appear to be any reason for using global variables for -
aINPUTcollection -, - x - or - oI -. This is a particularly dangerous
habit when it comes to loop counters, but still dubious otherwise. They
should all be function local variables. The general programming axiom
that variables should never be given more scope than is absolutely
necessary should be observed as rigorously in javascript as any other
language.
if(oI.type="button")if(!oI.onclick);
oI.onclick=new Function("return myClick(this)")
<snip>

It is really chunky and inefficient to be creating a new function object
for each button when you could just defined - myClick - as:-

function myClick(){
alert('Under Construction '+this.name);
}

- and assign references to that one function object to each button
element's - onclick - handler as:-

oI.onclick = myClick;

Richard.
 
S

Stephen Chalmers

Well, making it the default behavior is not too hard.

The hard part is convincing the lecturer that you wrote it yourself; that's assuming that they're still bothering to ask
these days.
 
W

wolfing1

Lee said:
Dr Clue said:

1. Please don't encourage people to use the <-- //--> comments in script.
Why is that? I'm just a dabbler in javascript and sometimes use it as
comments. What's wrong with doing it?
 
L

Lee

(e-mail address removed) said:
Why is that? I'm just a dabbler in javascript and sometimes use it as
comments. What's wrong with doing it?

If you're adding comments to your script, use the // syntax.
The SGML comment characters are not really part of Javascript.
If you use them to "hide code from old browsers", you're wasting
your time, because the percentage of such old browsers on the
internet dropped below 0.000 several years ago.
 
W

wolfing1

Lee said:
(e-mail address removed) said:

If you're adding comments to your script, use the // syntax.
The SGML comment characters are not really part of Javascript.
If you use them to "hide code from old browsers", you're wasting
your time, because the percentage of such old browsers on the
internet dropped below 0.000 several years ago.
Oh wait wait, I was confused, thought he meant using <!-- in HTML for
comments (double confusion on my part, yeah I know I suck).
Yeah for javascript I use // and /* .. */
 

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

No members online now.

Forum statistics

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

Latest Threads

Top