GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Javascript experts? (https://gfy.com/showthread.php?t=965075)

Jakez 04-24-2010 02:13 AM

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']);
}

redwhiteandblue 04-24-2010 04:10 AM

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.

seeandsee 04-24-2010 04:21 AM

bump for J-S mags

nation-x 04-24-2010 04:51 AM

Quote:

Originally Posted by Jakez (Post 17068770)
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>


Jakez 04-24-2010 06:29 AM

Quote:

Originally Posted by nation-x (Post 17069018)
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!

Jakez 04-24-2010 09:43 AM

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..

dozey 04-24-2010 11:28 AM

Code:

<a onclick="test({name:'Item Name', price:4.95});">click</a>

function test(item)
{
    alert(item.name + ': $' + item.price.toFixed(2));
}


Jakez 04-25-2010 12:40 AM

Quote:

Originally Posted by dozey (Post 17070353)
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 :error


All times are GMT -7. The time now is 03:20 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc