473,698 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using array values in onclick event

Can a javascript array value be used in an onclick event?
I haven't been successful in getting it to work so I'm wondering if it
even can be done.

onclick="calcul ate('some array value');

I've tried:
onclick="calcul ate('<script
type="text/javascript">doc ument.write(poi nts[0]);</script>');

There's conflict with the quotes, but beyond that, I'm unclear if this
is even a viable option...anyone ?

Jul 23 '05 #1
9 8106
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
su****@gmail.co m enlightened us with...
Can a javascript array value be used in an onclick event?
I haven't been successful in getting it to work so I'm wondering if it
even can be done.

onclick="calcul ate('some array value');

I've tried:
onclick="calcul ate('<script
type="text/javascript">doc ument.write(poi nts[0]);</script>');

There's conflict with the quotes, but beyond that, I'm unclear if this
is even a viable option...anyone ?


onClick is already a javascript event. Ditch the document.write stuff. And
the variable has to be global.

onClick="calcul ate(points[0]);"

--
--
~kaeli~
A chicken crossing the road is poultry in motion.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
Lee
DesignerNut said:

Can a javascript array value be used in an onclick event?
I haven't been successful in getting it to work so I'm wondering if it
even can be done.

onclick="calcu late('some array value');

I've tried:
onclick="calcu late('<script
type="text/javascript">doc ument.write(poi nts[0]);</script>');

There's conflict with the quotes, but beyond that, I'm unclear if this
is even a viable option...anyone ?


There's nothing special about Arrays that would prevent them
from being used in any particular event handler.

Using document.write( ) in an event handler is often a mistake,
because it clears the current document before writing.

Jul 23 '05 #3
Thanks a great deal! And for the explanations.

Jul 23 '05 #4
kaeli wrote:
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
su****@gmail.co m enlightened us with...
Can a javascript array value be used in an onclick event?
I haven't been successful in getting it to work so I'm wondering if it
even can be done.

onclick="calc ulate('some array value');

I've tried:
onclick="calc ulate('<script
type="text/javascript">doc ument.write(poi nts[0]);</script>');

There's conflict with the quotes, but beyond that, I'm unclear if this
is even a viable option...anyone ?

onClick is already a javascript event. Ditch the document.write stuff. And
the variable has to be global.

onClick="calcul ate(points[0]);"


Not to be contrary, but the array just has to be declared somewhere
that is accessible to the script - it /could/ be local:

... onClick="
var points = ['0','1','2'];
calculate(point s[0]);
" ...

or declared as a global in a script element:

<script type="text/javascript">
var points = ['0','1','2'];
</script>

... onClick="calcul ate(points[0]);" ...
or in some function sans 'var' if it needs to be accessible outside
the function:

... onClick="
points = ['0','1','2'];
calculate(point s[0]);
" ...

--
Rob
Jul 23 '05 #5
In article <42************ ***********@per-qv1-newsreader-01.iinet.net.au >,
rg***@iinet.net .auau enlightened us with...

or in some function sans 'var' if it needs to be accessible outside
the function:


That makes it global?
If so, I learned something new today. :)

--
--
~kaeli~
A plateau is a high form of flattery.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #6
kaeli wrote:
In article <42************ ***********@per-qv1-newsreader-01.iinet.net.au >,
rg***@iinet.net .auau enlightened us with...
or in some function sans 'var' if it needs to be accessible outside
the function:

That makes it global?
If so, I learned something new today. :)


Yes. It's probably easiest to remember that using 'var' inside a
function makes the variable local, anything else makes it global,
i.e.:

Declaring variables inside a function without the 'var' keyword
makes them global.

Declaring variables outside a function makes them global whether
'var' is used or not, so the 'var' keyword is probably redundant in
that case (but its use is encouraged by some - e.g. me - who think
it makes maintenance easier).

From ECMA 262 12.2:

"VariableStatem ent:
var VariableDeclara tionList;

...

"If the variable statement occurs inside a Function Declaration, the
variables are defined with function-local scope in that function...
Otherwise they are defined with global scope."

Note that 'VariableStatem ent' includes the 'var' keyword, without it
it's just a VariableDeclara tionList and will be global.

Specs are fun, eh? :-x

--
Rob
Jul 23 '05 #7
In article <r3************ ****@news.optus .net.au>, rg***@iinet.net .auau
enlightened us with...
From ECMA 262 12.2:
<snip> Specs are fun, eh? :-x


Can I rely on ECMA specs for cross-browser JS?
I'd love to have an actual reference that is at least accurate for the
majority of browsers...

I was under the impression MSIE used its own implementation of it (Jscript).
How closely (if you know) do they follow ECMA specs? Can I rely on ECMA specs
to be accurate for at least MSIE5+ *and* Gecko-based browsers?

--
--
~kaeli~
Experience is something you don't get until just after you
need it.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #8
kaeli <ti******@NOSPA M.comcast.net> writes:
Can I rely on ECMA specs for cross-browser JS?
Pretty much.
I'd love to have an actual reference that is at least accurate for the
majority of browsers...
It should be. If not, the browser vendor will consider it a bug (no
matter how little that helps you :)
I was under the impression MSIE used its own implementation of it (Jscript).
ECMAScript was designed as a standardization of (the core of) existing
languages, namely Netscape Corporation's JavaScript and Microsoft
Corporation's JScript.

They already agreed on many things (which is why ECMAScript was possible
to define), and where they didn't, it was mostly in what is now called
the DOM - the environment around the ECMAScript compatible scripting
language.

Both JavaScript (Netscape/Mozilla) and JScript are ECMAScript v3
compatible in their current versions (as are Opera and KHTML's
scripting languages). There might be a bug, but it will be in a
rarely used corner of the language.

(then again, IE fails on "[42,].length == 1", which should be true).
How closely (if you know) do they follow ECMA specs? Can I rely on
ECMA specs to be accurate for at least MSIE5+ *and* Gecko-based
browsers?


Pretty darned close. I'd depend on it, but be ready to double check
when something fails. No matter what you do, you should always test in
as many browsers as possible.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #9
RobG wrote:

[Variable assignment in intrinsic script]
or in some function sans 'var' if it needs to be accessible outside
the function:
That makes it global?
Yes.


That's true for Gecko based browsers, but not really for MSIE. Just try
out the following:

<form><p>
<input type="button" name="bar" onclick="foo=1"
value="Execute& quot;foo=1&quot ;"><br>
<input type="button" onclick="alert( window.foo)"
value="Check global foo property"><br>
</p></form>

As you'll see, when clicking the first, then the second button, in MSIE
"undefined" is alerted. That means, in MSIE, there is no global property
"foo" created. In Geckos, "1" is alerted, so there we have a global
"foo". For cross-browser scripting one should prefer
onclick="var foo=1";
to
onclick="foo=1" ;

BTW: I didn't find out to which object MSIE adds the foo property here,
but it must be one in both inputs' scope chains in the following example:

<form><p>
<input type="button" name="bar" onclick="foo=1"
value="Execute& quot;foo=1&quot ;"><br>
<input type="button" onclick="alert( foo)"
value="Execute& quot;alert(foo) &quot;"><br>
</p></form>

ciao, dhgm
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1239
by: Stephen | last post by:
I was wondering if someone could please help me with an array I'd like to create in an asp.net page. I have to design an array which stores the values of addresses manually entered into textboxes (txtAdd,txtCity&txtPostcode). The array must hold the values of these three textboxes. I would like the array to also display these address values in an asp:Lable within a asp:TableCell inside a asp:Table. I want all this to happen on the...
1
2169
by: Dark Magician | last post by:
Comrades: Am trying to build a UI widget. I'm sure part of the problem is proper variable scope or object reference, and part of the problem may be the way I'm calling the function, but, here goes. Part I. Consider: A B C D M E F G H N O
1
1816
by: Stephen | last post by:
Was wondering if someone could please help me with an array I'd like to create in an asp.net page. I have to design an array which stores the values of addresses manually entered into textboxes (txtAdd,txtCity&txtPostcode). The array must hold the values of these three textboxes. I would like the array to also display these address values in an asp:Lable within a asp:TableCell inside a asp:Table. I want all this to happen on the...
4
5390
by: Rich | last post by:
Hello, I create multiple pictureboxe controls on a form in a For Loop and display thumbnail pictures. I need to add a Click event to these pictureboxes. Here is the routine that creates the pictureboxes: ---------------------------------------------------------------------------- Dim MynewPos As New Point(50, 40) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
2
7710
by: bay_dar | last post by:
Hi, I have an internal ASP.NET application that I'm are using to send e-mails out based on a single milepost or milepost range entered. I'm trying to do two things when a user clicks on the submit button to submit a form that contains one or two Mileposts: 1) If a Milepost range larger than 5 miles is entered, I would like to pop up a confirmation box to confirm the range.
7
5062
by: Darko | last post by:
Hello, I have this particular problem with eval() when using Microsoft Internet Explorer, when trying to define an event handler. This is the code: function BigObject() { this.items = new Array(); this.values = new Array();
6
1821
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool. One of the requirements is that the calendar will need to display a varying number of months (1..3)
1
4078
by: sourcie | last post by:
I am changing an existing quiz found on "JavaScriptKit.com Multiple Choice Quiz" I have an image. Instead of using the radio buttons with the normal true/false question, I want to place two hotspots on the image. One being correct(a) and the other incorrect(b). When the user clicks on the correct hotspot or place on the image, it should score and retain that value until the end of the quiz. At the end of the quiz, there is a submit button...
3
2395
by: swetha123 | last post by:
hello, I don't know how to use cookies please help me in this I am using the dream weaver cs4 I designed the navigation bar to my page using dream weaver cs4 navigation bar contains Home, Retail Contact Us
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8855
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7708
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.