Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 04-24-2010, 02:13 AM   #1
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
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']);
}
__________________
[email protected] - jakezdumb - 573689400

Killuminati

Last edited by Jakez; 04-24-2010 at 02:16 AM..
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-24-2010, 04:10 AM   #2
redwhiteandblue
Bollocks
 
redwhiteandblue's Avatar
 
Industry Role:
Join Date: Jun 2007
Location: Bollocks
Posts: 2,792
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.
redwhiteandblue is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-24-2010, 04:21 AM   #3
seeandsee
Check SIG!
 
seeandsee's Avatar
 
Industry Role:
Join Date: Mar 2006
Location: Europe (Skype: gojkoas)
Posts: 50,945
bump for J-S mags
__________________
BUY MY SIG - 50$/Year

Contact here
seeandsee is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-24-2010, 04:51 AM   #4
nation-x
Confirmed User
 
nation-x's Avatar
 
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
Quote:
Originally Posted by Jakez View Post
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>
nation-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-24-2010, 06:29 AM   #5
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by nation-x View Post
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
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-24-2010, 09:43 AM   #6
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
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
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-24-2010, 11:28 AM   #7
dozey
Confirmed User
 
dozey's Avatar
 
Join Date: Nov 2004
Location: Moonland
Posts: 552
Code:
<a onclick="test({name:'Item Name', price:4.95});">click</a>

function test(item)
{
    alert(item.name + ': $' + item.price.toFixed(2));
}
dozey is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-25-2010, 12:40 AM   #8
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by dozey View Post
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
__________________
[email protected] - jakezdumb - 573689400

Killuminati

Last edited by Jakez; 04-25-2010 at 12:41 AM..
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.