Solved

How can I use a Javascript function parameter to choose an array element?

Posted on 2015-01-19
6
100 Views
Last Modified: 2015-01-23
Hi, ‪‎Javascript‬ friends!
How can I use a function parameter to choose an array element?

Example:

function populateSelect(id_select, function_name, field) {
if ($('#'+id_select).size() == 1) {
$.post('php/db_functions.php', 'function='+function_name, function(data, textStatus) {
elements = data.items;
$.each(elements, function(index, elements) {
$('#'+id_select).append('<option value="' + elements.id + '">' + elements.field + '</option>'); // I would like to use in this row the field parameter
});
$('#'+id_select).trigger('change');
}, "json");
}
}

Open in new window

0
Comment
Question by:ltpitt
  • 3
  • 3
6 Comments
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
Change elements.field to:

field[index]
0
 
LVL 1

Author Comment

by:ltpitt
Comment Utility
I don't get it...

Can you please be more explicit with code example?
0
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
Maybe I misunderstood the question. You are passing the field parameter, an array according to your question, to the populateSelect() function. The comment in the $.each loop says you want to use the field parameter in the row. You had <option value="' + elements.id + '">' + elements.field + '</option>. You are using "field" as if it were a property of "elements". If field is an array then you must use an index to access the items inside. You have assigned the variable "index" to represent the index of the $.each loop. So field[index] will access the items in the field array. Caution: if elements.length is more than field.length, you are going to get an error.
function populateSelect(id_select, function_name, field) {
if ($('#'+id_select).size() == 1) {
$.post('php/db_functions.php', 'function='+function_name, function(data, textStatus) {
elements = data.items;
$.each(elements, function(index, elements) {
$('#'+id_select).append('<option value="' + elements.id + '">' +field[index] + '</option>'); // I would like to use in this row the field parameter
});
$('#'+id_select).trigger('change');
}, "json");
}
}

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:ltpitt
Comment Utility
I'm sorry but I see I was unclear...

All I wanto to do is populate 3 select in my html page.

Each select's array comes from a different table from the database.

From the sake of learning and writing better code I'd like to write a single populateSelect function instead of writing 3 specific function (one for each select to populate).

For this reason the "field" parameter would be the column name i'd need to get from the db...

I hope I was clearer this time: sorry for wasting your time :(
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
Comment Utility
You can pass whatever data you need through the post using name:value pairs.

$.post('php/db_functions.php', {  function_name : field }, function(data, textStatus) {

    ...

)};

Then test for the names in the $_POST array on the php side. Let's say the function name passed is 'getData'. It's value will be the field name you want to pass to the php function.

<?php

if (isset($_POST['getData'])) {
       //call the actual getData() function
        getData($_POST['getData']);
}

function getData(field) {
}

?>
0
 
LVL 1

Author Closing Comment

by:ltpitt
Comment Utility
oh so you'd pass right from the php just the right data I need.

So simple I feel stupid: thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now