473,323 Members | 1,550 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,323 software developers and data experts.

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="calculate('some array value');

I've tried:
onclick="calculate('<script
type="text/javascript">document.write(points[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 8082
In article <11**********************@o13g2000cwo.googlegroups .com>,
su****@gmail.com 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="calculate('some array value');

I've tried:
onclick="calculate('<script
type="text/javascript">document.write(points[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="calculate(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="calculate('some array value');

I've tried:
onclick="calculate('<script
type="text/javascript">document.write(points[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**********************@o13g2000cwo.googlegroups .com>,
su****@gmail.com 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="calculate('some array value');

I've tried:
onclick="calculate('<script
type="text/javascript">document.write(points[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="calculate(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(points[0]);
" ...

or declared as a global in a script element:

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

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

... onClick="
points = ['0','1','2'];
calculate(points[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:

"VariableStatement:
var VariableDeclarationList;

...

"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 'VariableStatement' includes the 'var' keyword, without it
it's just a VariableDeclarationList 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******@NOSPAM.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/rasterTriangleDOM.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
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...
1
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...
1
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...
4
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...
2
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...
7
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...
6
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....
1
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...
3
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.