1

I have an array contains many option tag like this

enter image description here

How do I concat them into a string and then make it into a select block?

Like this

var select_block = '<select id="mission_reward" name="mission_reward" class="select_reward">';

// Maybe write something to concat array into string ?
select_block = select_block + options;  // options is an array contains many option tags
select_block = select_block + '</select>';
1
  • Do you want a string at the end, or an actual select object with those options? And does your array contain strings describing the options, or actual option elements from your document? Commented Feb 2, 2017 at 15:37

4 Answers 4

2

To create a string of HTML, using non-jQuery JavaScript, you could use the following approaches:

// here we create a string of HTML, using a String literal,
// and concatenate that String with another:
var html = '<select id="mission_reward" name="mission_reward" class="select_reward">' +

  // here we use Array.prototype.join() to combine the
  // Array elements of the options Array (an Array of
  // Strings) together using a zero-length string:
  options.join('') +

  // and then concatenate the String-so-far with the final
  // String to close the resulting <select> element:
  '</select>';

// creating the options Array:
var options = ['<option name="demo">opt1</option>', '<option name="demo">opt2</option>', '<option name="demo">opt3</option>']


var html = '<select id="mission_reward" name="mission_reward" class="select_reward">' + options.join('') + '</select>';

document.body.insertAdjacentHTML('beforeend', html);

JS Fiddle demo.

This is only possible if the options Array elements are strings, though. If they're DOM nodes, then you can use the following instead (to, again, create a string of HTML):

// similarly to the above approach, we have a String literal
// to open the resulting <select> element, and then we use
// Array.prototype.join() to create a String from the
// options Array-elements:
var html = '<select id="mission_reward" name="mission_reward" class="select_reward">' +

  // here we have an Array of HTMLOptionElements, which we must
  // convert to Strings, comprised of their own HTML, so
  // we use an Arrow function to change the Array,
  // map takes the current Array-element ('opt') and returns
  // the outerHTML of that node to the Array, and then the
  // resulting Array of HTML-Strings is joined together to
  // form an HTML-String:
  options.map( opt => opt.outerHTML ).join('') +

  // and again closing the resulting <select> element:
  '</select>';

// creating the options Array:
var options = new Array(3).fill('').map(function(_, i) {
  var o = document.createElement('option');
  o.value = 'demo';
  o.text = 'opt' + i;
  return o;
});

var html = '<select id="mission_reward" name="mission_reward" class="select_reward">' + options.map(opt => opt.outerHTML).join('') + '</select>';

document.body.insertAdjacentHTML('beforeend', html);

However it's often better to use the DOM to create Nodes that can be inserted into the document, rather than creating strings. To use the DOM API approach of creating nodes, the following is possible (albeit more verbose):

// here we directly create a <select> element:
var selectElement = document.createElement('select');

// set its 'id' and 'name' to the String of 'mission_reward':
selectElement.id = selectElement.name = 'mission_reward';

// add the 'select_reward' class-name:
selectElement.classList.add('select_reward');

// iterate over the options Array and, on each iteration,
// append the current <option> ('opt') to the created 
// <select> element:
options.forEach( opt => selectElement.appendChild(opt) );

// creating the options Array:
var options = new Array(3).fill('').map(function(_, i) {
  var o = document.createElement('option');
  o.value = 'demo';
  o.text = 'opt' + i;
  return o;
});

var selectElement = document.createElement('select');

selectElement.id = selectElement.name = 'mission_reward';
selectElement.classList.add('select_reward');

options.forEach(opt => selectElement.appendChild(opt));

document.body.appendChild(selectElement);

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

Comments

1

You can use jQuery()

var select = $("<select></select>", {
               id: "mission_reward",
               name: "mission_reward",
               "class": "select_reward",
               html: options
             });

Comments

0

If you're using jQuery you don't need to concatenate them in to a string at all. You can just append the array of DOMElements are they are:

var options = [ /* your option elements */ ];
var $select_block = $('<select id="mission_reward" name="mission_reward" class="select_reward" />');
$select_block.append(options);

Comments

0

You want a select with an option for each of the options of the array ? Then you surely want to apply a forEach() method:

var opt = ['option A', 'Option B', 'Option C'];
var select_block = '<select id="mission_reward" name="mission_reward" class="select_reward">';

opt.forEach(function(element) {
    // crude code follows ;)
    select_block +="<option value='"+element+"'>"+element+"</option>";
});
select_block +="</select>";

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.