Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-19
6
Medium Priority
?
112 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
ID: 40558663
Change elements.field to:

field[index]
0
 
LVL 1

Author Comment

by:ltpitt
ID: 40558743
I don't get it...

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

Expert Comment

by:Tom Beck
ID: 40558837
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ltpitt
ID: 40563953
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 2000 total points
ID: 40564099
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
ID: 40565960
oh so you'd pass right from the php just the right data I need.

So simple I feel stupid: thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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 …
Suggested Courses

886 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