473,796 Members | 2,688 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 8112
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
1246
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
2174
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
1820
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
5393
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
7718
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
5065
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
1828
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
4086
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
2405
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
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
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
9050
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
6785
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();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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.