---
title: "How to call function with variable as paramenter in jquery?"  
description: "How to call function with variable as paramenter in jquery?"  
author: "Jayden Bell"  
published: 2015-03-18  
updated: 2015-03-18  
canonical: https://www.mindstick.com/forum/23029/how-to-call-function-with-variable-as-paramenter-in-jquery  
category: "javascript"  
tags: ["jquery", "jquery selectors"]  
reading_time: 2 minutes  

---

# How to call function with variable as paramenter in jquery?

I want to call the [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) fill() with a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) as parameter. Everytime next() is called, the number of the variable should increase. So first, fill( q1 ) is called, then fill( q2 ) and so on. But apparently, you can't use [variables](https://www.mindstick.com/articles/715/php-variables) as parameters. Any [ideas](https://www.mindstick.com/articles/43878/making-the-best-use-of-the-options-trade-ideas)?

```
var q1 = [2, "ff", "dd", "ss", "hh"];    var q2 = [2, "ff", "dd", "ss", "hh"];     var fill = function (data) {        $("#number").html(data[1]);        $("#cat").html(data[2]);        $("#ques span").html(data[3]);        $("#answ .answ:nth-child(1) button").html(data[4]);        $("#answ .answ:nth-child(2) button").html(data[5]);        $("#answ .answ:nth-child(3) button").html(data[6]);        $("#answ .answ:nth-child(4) button").html(data[7]);        $("#answ .answ:nth-child(" + data[0] + ") button").attr("data-state", "correct");    }    var count = 1;    function next() {        fill("q" + count);        count++;    }
```

## Replies

### Reply by Royce Roy

I would think a better approach is to make a multi-dimensional array q, in stead of q1 and q2 etc.

```
var q = [];    q[0] = [2, "ff", "dd", "ss", "hh"];    q[1] = [2, "ff", "dd", "ss", "hh"];     var fill = function (data) {        $("#number").html(data[1]);        $("#cat").html(data[2]);        $("#ques span").html(data[3]);        $("#answ .answ:nth-child(1) button").html(data[4]);        $("#answ .answ:nth-child(2) button").html(data[5]);        $("#answ .answ:nth-child(3) button").html(data[6]);        $("#answ .answ:nth-child(4) button").html(data[7]);        $("#answ .answ:nth-child(" + data[0] + ") button").attr("data-state", "correct");    }    var count = 0;    function next() {        fill(q[count]);        count++;    }
```

This way, you could also check that you're not passing undefined to fill():

```
function next() {        if (count < q.length) {            fill(q[count]);            count++;        }    }
```


---

Original Source: https://www.mindstick.com/forum/23029/how-to-call-function-with-variable-as-paramenter-in-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
