Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-19
6
Medium Priority
?
110 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

715 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