Hello,
How to return an array from a Javascript function ?
For eg: - function test()
-
{
-
var test1 = document.getElementsByName("textarea");
-
-
return test1;
-
}
Please correct the above code.
22 5816
If you want an array of the textareas on the page, change getElementsByName to getElementsByTagName.
If you want an array of the textareas on the page, change getElementsByName to getElementsByTagName.
No I want to extract textareas which have a specific name.
Also tell me how to return associative arrays ?
Then replace "textarea" in your original code to the name of the textareas.
Then replace "textarea" in your original code to the name of the textareas.
That I am already doing .
Also tell me how to return an object.
For eg: - var obj = new Object();
-
obj["firstName"]=”vijay”;
-
obj["lastName"]=”khambalkar”;
If it's an array, use [] in place of "new Object()" and then after those statements, return obj; will return an array.
If it's an array, use [] in place of "new Object()" and then after those statements, return obj; will return an array.
No I want to know how to return associative arrays ?
What does the following do? - <html>
-
<head>
-
<script>
-
function test() {
-
var obj = [];
-
obj["firstName"]="vijay";
-
obj["lastName"]="khambalkar";
-
return obj;
-
}
-
</script>
-
</head>
-
<body>
-
<script>
-
var t = test();
-
document.write(t["firstName"]);
-
</script>
-
</body>
-
</html>
What does the following do? - <html>
-
<head>
-
<script>
-
function test() {
-
var obj = [];
-
obj["firstName"]="vijay";
-
obj["lastName"]="khambalkar";
-
return obj;
-
}
-
</script>
-
</head>
-
<body>
-
<script>
-
var t = test();
-
document.write(t["firstName"]);
-
</script>
-
</body>
-
</html>
I want to send the array to a PHP file using Ajax.
How to read the array in PHP ?
I want to send the array to a PHP file using Ajax.
How to read the array in PHP ?
Now it makes more sense what you're trying to do.
Send it as a string with a unique delimiter and then split the string using that character with PHP.
Now it makes more sense what you're trying to do.
Send it as a string with a unique delimiter and then split the string using that character with PHP.
For sending it as a string I have to traverse through the array, take each value and concatenate it.
Is there any other way ?
Use the join() method to put all the elements of an array into a string.
Use the join() method to put all the elements of an array into a string.
But in that case will I get the keys also ?
Its an associative array.
Do you need the keys too?
Since it's an associative array/object, use a for...in loop: - var obj = new Object;
-
obj["whatever"] = "something";
-
...
-
var str = ""
-
for (var i in obj)
-
str+=obj[i] + ":";
-
Do you need the keys too?
Since it's an associative array/object, use a for...in loop: - var obj = new Object;
-
obj["whatever"] = "something";
-
...
-
var str = ""
-
for (var i in obj)
-
str+=obj[i] + ":";
-
I know this way.
I wanted a way to return the associative array.
If you need the keys too, add them to the string too, e.g. - for (var i in obj) {
-
str += i + ":" + obj + "|";
-
}
- var aArray = new Array;
-
aArray['key'] = new Array;
-
aArray['value'] = new Array;
-
var key;
-
for(key in yourArray) {//yourArray is your original array
-
aArray['value'].push(yourArray[key]);
-
aArray['key'].push(key);
-
}
Hope you could utilize aArray
If you need the keys too, add them to the string too, e.g. - for (var i in obj) {
-
str += i + ":" + obj + "|";
-
}
I don't want the values in a string.
I just want to return the associative array.
- var aArray = new Array;
-
aArray['key'] = new Array;
-
aArray['value'] = new Array;
-
var key;
-
for(key in yourArray) {//yourArray is your original array
-
aArray['value'].push(yourArray[key]);
-
aArray['key'].push(key);
-
}
Hope you could utilize aArray
You are creating two arrays.
I want a single associative array.
I don't want the values in a string.
I just want to return the associative array.
Tell me one thing first...
What is an associative array? - arr[0] = 'a'
-
arr[1] = 'b'
-
arr[2] = 'c'
-
arr[3] = 'd'
-
arr[4] = 'e'
If this is an array, what results do you want?
I don't want the values in a string.
I just want to return the associative array.
We already did that earlier with the return statement. I thought you wanted to pass this to a PHP script via Ajax.
Tell me one thing first...
What is an associative array? - arr[0] = 'a'
-
arr[1] = 'b'
-
arr[2] = 'c'
-
arr[3] = 'd'
-
arr[4] = 'e'
If this is an array, what results do you want?
Actually I want to send an associative array to a PHP file via Ajax without converting it to a string.
arr["asasa"] = 'a'
arr["wew"] = 'b'
arr["rtr"] = 'c'
Okey... if that is the case, you have to send it as a string only.
In PHP, you can use explode to convert your string back to an array.
Use what acoder suggested in post#16.
Then when your variables is received by your PHP script.
Use:[php]$your_final_array("key"=>array(), "value"=>array());
$your_array = explode(",", $_POST['your_array_string']); //or GET... which ever you using
foreach ($your_array as $each_value)
{
array_push($your_final_array['key'], strtok($each_value, ":"));
array_push($your_final_array['value'],strtok(":"));
}[/php]
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Don |
last post by:
I want the server-side php script to return a browser page that is essentially a copy of the
original client page that contained the <form> which referenced the php script in the first place....
|
by: Mike Copeland |
last post by:
In the following code I am calculating 2 values when the "Compute
Distance" button is pressed. I would like to find a way to "return" both
value to the form so I can show both when the calculation...
|
by: strout |
last post by:
function F(e)
{
return
function(){P(e)}
}
Can anybody tell me what the code is doing?
If return another function all in a function I would do
function F(e)
|
by: Andrew Poulos |
last post by:
If I'm searching for an occurance of a value in a multi-dimensional
array how can I get it's index returned as an array, if found? For
example, if:
foo = new Array();
foo = , 5, , 9, 10];
...
|
by: BrianP |
last post by:
Hi,
I have had to invent a work-around to get past what looks like a
JavaScript bug, the malfunctioning Perl-like JavaScript array functions
including SPLICE() and UNSHIFT().
I have boiled it...
|
by: VK |
last post by:
Hello,
In my object I have getDirectory() method which returns 2-dimentional
array
(or an imitation of 2-dimentional array using two JavaScript objects
with auto-handled length property - please...
|
by: mflll |
last post by:
I am looking into the different techniques of handling arrays of edit
boxes in
Java Script. The first program below works fine. However, are there
better ways of
doing this, where the person...
|
by: SM |
last post by:
Hello
I'm trying to create a multi dimensional array in JavaScript, but
after some reading i still can't figure out how to apply it to my
model.
Here it is:
I have a list A and for each item...
|
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= |
last post by:
Hey guys, I've searched high and low for a way to populate a vb array with
data from a javascript array. I can find 50 ways to do it with ASP but not
one for VB. I appreciate what help you can...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |