using array values in onclick event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DesignerNut

    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 ?

  • kaeli

    #2
    Re: using array values in onclick event

    In article <1112625508.714 193.155890@o13g 2000cwo.googleg roups.com>,
    sundog@gmail.co m enlightened us with...[color=blue]
    > 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 ?
    >[/color]

    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.



    Comment

    • Lee

      #3
      Re: using array values in onclick event

      DesignerNut said:[color=blue]
      >
      >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 ?[/color]

      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.

      Comment

      • DesignerNut

        #4
        Re: using array values in onclick event

        Thanks a great deal! And for the explanations.

        Comment

        • RobG

          #5
          Re: using array values in onclick event

          kaeli wrote:[color=blue]
          > In article <1112625508.714 193.155890@o13g 2000cwo.googleg roups.com>,
          > sundog@gmail.co m enlightened us with...
          >[color=green]
          >>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 ?
          >>[/color]
          >
          >
          > onClick is already a javascript event. Ditch the document.write stuff. And
          > the variable has to be global.
          >
          > onClick="calcul ate(points[0]);"
          >[/color]

          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

          Comment

          • kaeli

            #6
            Re: using array values in onclick event

            In article <42515b7d$0$191 47$5a62ac22@per-qv1-newsreader-01.iinet.net.au >,
            rgqld@iinet.net .auau enlightened us with...[color=blue]
            >
            > or in some function sans 'var' if it needs to be accessible outside
            > the function:
            >[/color]

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

            --
            --
            ~kaeli~
            A plateau is a high form of flattery.



            Comment

            • RobG

              #7
              Re: using array values in onclick event

              kaeli wrote:[color=blue]
              > In article <42515b7d$0$191 47$5a62ac22@per-qv1-newsreader-01.iinet.net.au >,
              > rgqld@iinet.net .auau enlightened us with...
              >[color=green]
              >> or in some function sans 'var' if it needs to be accessible outside
              >> the function:
              >>[/color]
              >
              >
              > That makes it global?
              > If so, I learned something new today. :)
              >[/color]

              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

              Comment

              • kaeli

                #8
                Re: using array values in onclick event

                In article <r3k4e.705$Zn.3 8222@news.optus .net.au>, rgqld@iinet.net .auau
                enlightened us with...[color=blue]
                > From ECMA 262 12.2:
                >[/color]
                <snip>[color=blue]
                > Specs are fun, eh? :-x[/color]

                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.



                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: using array values in onclick event

                  kaeli <tiny_one@NOSPA M.comcast.net> writes:
                  [color=blue]
                  > Can I rely on ECMA specs for cross-browser JS?[/color]

                  Pretty much.
                  [color=blue]
                  > I'd love to have an actual reference that is at least accurate for the
                  > majority of browsers...[/color]

                  It should be. If not, the browser vendor will consider it a bug (no
                  matter how little that helps you :)
                  [color=blue]
                  > I was under the impression MSIE used its own implementation of it (Jscript).[/color]

                  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).
                  [color=blue]
                  > 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?[/color]

                  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 - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Dietmar Meier

                    #10
                    Re: using array values in onclick event

                    RobG wrote:

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

                    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

                    Comment

                    Working...