www.webdeveloper.com
Results 1 to 2 of 2

Thread: Add var to array

  1. #1
    Join Date
    May 2013
    Posts
    2

    Add var to array

    Hello,
    i Have the function below to ad a certain amount of dropdown lists to a form.
    It works like a charm but i want to add the input_id var to the dropdown list created in the "data" array
    "'_1': '13 x 18 cm'," must become "input_id '_1': '13 x 18 cm',"

    I can't get it together.

    function getval(sel) {
    var input=sel.value.split("_");
    var input_aant = input[1];
    var input_id = input[0];
    var input_name = "#"+input[0];

    $(input_name).empty();

    for (i=1; i<=input_aant; i++)
    {
    var data = {
    '_1': '13 x 18 cm',
    '_2': '20 x 30 cm',
    '_3': '40 x 60 cm',
    '_4': '50 x 75 cm'
    }

    var g = $('<div class="formaat"/>');
    var s = $('<select onchange=\"getalert(this);\" />');

    for(var val in data) {
    $('<option />', {value: val, text: data[val]}).appendTo(s);
    }

    g.appendTo(input_name);
    s.appendTo(input_name);
    }
    }

  2. #2
    Join Date
    Jul 2013
    Posts
    66
    Please format your code as such to maintain intendation and readability and consider providing a working example ("working" as in "shows the problem") for future questions as it helps us to understand the problem and in return help you.

    As for your question, you could either add the input_id in the loop where you create the <option /> elements (since input_id is constant for this function anyway), or if it absolutely has to go into the data object:

    Code:
    var data = {};
    data[input_id + '_1'] = "13 x 18cm";
    // ...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles