By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

giving buttons the same onclick event

P: n/a
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.

Oct 3 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
> and it has to be as easy as giving each button their
own onclick attribute otherwise I'll just do that.


Yes just do that !

onclick = "event()"
Oct 3 '05 #2

P: n/a
drawde83 wrote:
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.getElementsByTagNam e("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>
--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> Internet Programming since 1994 <>=-- DHTML NSAPI TCP/IP
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Oct 3 '05 #3

P: n/a
Lee
Dr Clue said:

drawde83 wrote:
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.getElementsByTagNam e("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()"/ ?

Oct 3 '05 #4

P: n/a
Lee wrote:
<snip>
1. Please don't encourage people to use the <-- //--> comments in script.
Old habits die real hard. Probably the only thing that would stop my
fingers from automaticly typing that line without me even thinking about
it would be if it stopped working.
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()"/ ?


This is a javascript newsgroup, so I give a javascript solution,
and besides it's a much more useful response in the long run.
--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> Internet Programming since 1994 <>=-- DHTML NSAPI TCP/IP
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Oct 3 '05 #5

P: n/a
Dr Clue wrote:
<snip>
<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.getElementsByTagNam e("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.
Oct 3 '05 #6

P: n/a
Dr Clue <ia*********@mindspring.com> wrote in message news:Xn*****************@newsread1.news.pas.earthl ink.net...
drawde83 wrote:
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.


The hard part is convincing the lecturer that you wrote it yourself; that's assuming that they're still bothering to ask
these days.
--
S.C.
Oct 4 '05 #7

P: n/a
Stephen Chalmers wrote:
Dr Clue <ia*********@mindspring.com> wrote in message news:Xn*****************@newsread1.news.pas.earthl ink.net...
drawde83 wrote:
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.


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


These days I think the concern ends with the cashing of the tuition
check, and that from there on your plugging up a seat that could be sold
again.

--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> Internet Programming since 1994 <>=-- DHTML NSAPI TCP/IP
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Oct 4 '05 #8

P: n/a

Lee wrote:
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?

Oct 5 '05 #9

P: n/a
Lee
wo******@gmail.com said:


Lee wrote:
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?


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.

Oct 5 '05 #10

P: n/a

Lee wrote:
wo******@gmail.com said:


Lee wrote:
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?


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 /* .. */

Oct 6 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.