Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

select option in js dynamically

const countries = {
  AF: 'Afghanistan',
  AL: 'Albania',
  HR: 'Croatia',
  ID: 'Indonesia',     // <<< make this one defaultSelected
  ZW: 'Zimbabwe',
};

const EL_country = document.querySelector('#country');
const DF_options = document.createDocumentFragment();

Object.keys(countries).forEach(key => {
  const isIndonesia = key === 'ID';  // Boolean
  DF_options.appendChild(new Option(countries[key], key, isIndonesia, isIndonesia))
});

EL_country.appendChild(DF_options);

document.forms[0].reset(); // "Indonesia" is still selected
Comment

PREVIOUS NEXT
Code Example
Javascript :: isodate mongodb nodejs 
Javascript :: fibbanacci sequence 
Javascript :: closure 
Javascript :: is an Angular component, then verify that it is part of this module. 
Javascript :: react setstate concat string 
Javascript :: before unload 
Javascript :: array 
Javascript :: javascript nullish 
Javascript :: obfuscate js string 
Javascript :: display none y display block infinito con javascript 
Javascript :: how to fetch api in class component react 
Javascript :: math module js 
Javascript :: regex exact match 
Javascript :: react filter array 
Javascript :: how to stop overlapping divs to interact with each others click events 
Javascript :: smtp testing server 
Javascript :: js variable to string 
Javascript :: convert javascript to ruby 
Javascript :: javascript array destructuring 
Javascript :: display toastr info 
Javascript :: simple search filter for table html 
Javascript :: node js login and registration 
Javascript :: how to add class in javascript dynamically 
Javascript :: Origin http://localhost:3002 is not allowed by Access-Control-Allow-Origin. 
Javascript :: nesting express routes 
Javascript :: dynamic input fields with radio button 
Javascript :: how to compare two strings in javascript if condition 
Javascript :: string.regex match 
Javascript :: sort array of objects javascript by properties value 
Javascript :: django add csrf token to formdata 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =