473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

giving buttons the same onclick event

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
10 1843
> 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
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
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
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
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
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
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

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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: amith | last post by:
hi, i have some 10 radio buttons meant to take the rating from the user. ex: 1 2 3 4 5 6 7 8 9 10 looks O O O O O O O O O O 1 2 3 4 5 6 7 8 9 10 features O O O O O O O O O O
6
by: Jon | last post by:
Hello, I have a datagrid and the data in it is dynamically created at runtime... For iCounter = 0 To dataset.Tables(0).Columns.Count - 1 Dim objbc As New BoundColumn() With objbc .DataField...
4
by: actionwoman63 | last post by:
Dear all I need to be able to check which one out of two submit buttons within the same form was pressed in a javascript function prior to form submission. And before I get flamed for not...
1
by: Hrvoje Vrbanc | last post by:
Hello all! My question is the following: I add buttons (server controls) programatically into a table cell. Example: Dim btObnovi As Button = New Button() celija25.Controls.Add(btObnovi) ...
1
by: Jerry | last post by:
We have a 10-question quiz for kids, each question being a yes or no answer using radio selections. I'd like to keep a current total of yes's and no's at the bottom of the quiz (if the user selects...
5
by: mayur_hirpara | last post by:
Hi, I have been developing web applications for a while now. However, as I was thinking through the architecture I really don't understand the "How server can identify between which buttons has...
6
by: Totto | last post by:
Hi, Anyone know the best solution to dynamically add buttons to a asp 2.0 page using data from Sql server? Are there any contols suitable for this or is it best to iterate the dataset and...
2
by: Ian :\) | last post by:
Hi Just learning JavaScript and I'm trying to reference radio buttons generated by php, the relevant lines are: <td width="9%" align="center" class="bg-tb-td"><input name="editArticle"...
3
by: COHENMARVIN | last post by:
I created asp.net pages that contained several controls, including buttons. I would double click on the button, and this would take me to the 'code-behind' page, and create the shell of a routine...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.