Javascript experts?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jakez
    Confirmed User
    • Jan 2004
    • 5656

    #1

    Javascript experts?

    How the hell would I pass an associative array to a javascript function?

    Shitty example:

    <a onclick="test(arr['item']='Item Name', arr['price']='4.95');">click</a>

    function test(arr)
    {
    alert(arr['item'] + arr['price']);
    }
    Last edited by Jakez; 04-24-2010, 01:16 AM.
    [email protected] - jakezdumb - 573689400

    Killuminati
  • redwhiteandblue
    Bollocks
    • Jun 2007
    • 2793

    #2
    I don't know of a syntax that will let you do it that way, maybe just pass it in as a string of key and value pairs and have your function build the array from them.
    Interserver unmanaged AMD Ryzen servers from $73.00

    Comment

    • seeandsee
      Check SIG!
      • Mar 2006
      • 50945

      #3
      bump for J-S mags
      BUY MY SIG - 50$/Year

      Contact here

      Comment

      • nation-x
        Confirmed User
        • Mar 2004
        • 5370

        #4
        Originally posted by Jakez
        How the hell would I pass an associative array to a javascript function?

        Shitty example:

        <a onclick="test(arr['item']='Item Name', arr['price']='4.95');">click</a>

        function test(arr)
        {
        alert(arr['item'] + arr['price']);
        }
        why not do it like this instead

        http://nation-x.com/test.html

        Code:
        <script type="text/javascript">
        var arr = new Object();
        
        function testIt() {
        	for (var i in arr) {
        		alert('arr[\''+i+'\'] is ' + arr[i])
        	}
        }
        </script>
        
        If you click <a href="#" onClick="arr.item='Item Name'; arr.price='4.95';testIt(); return false;">this link</a> this script is executed:</p>

        Comment

        • Jakez
          Confirmed User
          • Jan 2004
          • 5656

          #5
          Originally posted by nation-x
          why not do it like this instead

          http://nation-x.com/test.html

          Code:
          <script type="text/javascript">
          var arr = new Object();
          
          function testIt() {
          	for (var i in arr) {
          		alert('arr[\''+i+'\'] is ' + arr[i])
          	}
          }
          </script>
          
          If you click <a href="#" onClick="arr.item='Item Name'; arr.price='4.95';testIt(); return false;">this link</a> this script is executed:</p>
          You're the man now dog! Thanks!
          [email protected] - jakezdumb - 573689400

          Killuminati

          Comment

          • Jakez
            Confirmed User
            • Jan 2004
            • 5656

            #6
            Fuck, if I run this more than once on a page then old arguments are still passed through, unless I re-define them, then it's all good, but if I'm not using one of them on the 2nd time it will still be there from the first one. I tried clearing everything each time the function is run but it still persists. Maybe I need to clear the object each time or something? Haven't tried that yet I gotta run somewhere real quick..
            [email protected] - jakezdumb - 573689400

            Killuminati

            Comment

            • dozey
              Confirmed User
              • Nov 2004
              • 552

              #7
              Code:
              <a onclick="test({name:'Item Name', price:4.95});">click</a>
              
              function test(item)
              {
                  alert(item.name + ': $' + item.price.toFixed(2));
              }

              Comment

              • Jakez
                Confirmed User
                • Jan 2004
                • 5656

                #8
                Originally posted by dozey
                Code:
                <a onclick="test({name:'Item Name', price:4.95});">click</a>
                
                function test(item)
                {
                    alert(item.name + ': $' + item.price.toFixed(2));
                }
                ooo that is neat, takes care of the persistent vars problem too, thanks dudes. The help here is better than any programming forum would do
                Last edited by Jakez; 04-24-2010, 11:41 PM.
                [email protected] - jakezdumb - 573689400

                Killuminati

                Comment

                Working...