0

I am looking to grab a string value from a text box and multiple values from a multiselect list and associate them where the string value is the key, and the multiple values from the drop down list are the values.

Here is the javascript code I have thus far:

var serviceName = document.getElementById('servicePackageText').value;
var sourceType = document.getElementById("multiple-checkboxes");

var groupName = serviceGroupName;
var serviceArray = new Array();
for (i = 0; i < sourceType.selectedOptions.length; i++) {
  serviceArray.push(parseInt(sourceType.selectedOptions[i].value));
}

I want the format to look like this:

"Textbox value": [  
  multiselect_values,
  multiselect_values,
  multiselect_values,
  multiselect_values
]

Any tips on how to proceed?

1
  • 1
    Please provide a runnable minimal reproducible example along with actual expected results. Click on <> in question editor to make it runnable within this page Commented Dec 14, 2018 at 18:50

1 Answer 1

1

You need wrapper object, then it is easy. All you have to do is use [] property accessor to assign the object property.

I took some liberty with your code to make it simpler to rationalize:

var serviceName = "myDog"; //mock document.getElementById('servicePackageText').value;
var sourceType = [1, 2, 3, 4]; //mock document.getElementById("multiple-checkboxes");

var groupName = {};
groupName[serviceName] = sourceType; // <--- THE ANSWER

console.log(groupName);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.