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

using array values in onclick event

P: n/a
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
Share this Question
Share on Google+
9 Replies


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

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

P: n/a
Thanks a great deal! And for the explanations.

Jul 23 '05 #4

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.