Last active
May 13, 2016 02:05
-
-
Save ameyamashiro/fda3a57d19493b1ae14305c0db8bb53b to your computer and use it in GitHub Desktop.
Generate Option tag string from ccomma-separated value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>カンマ区切りで入力した値で option タグを生成。</p> | |
<textarea id="input-area" cols="30" rows="10"></textarea> | |
<button>Generate</button> | |
<div class="ret"></div> | |
<script> | |
// extract option list array from comma separated text | |
var extractArray = function() { | |
var t = []; | |
document.getElementById('input-area').value.split(',').forEach(function(e) { | |
t.push(e.trim()) | |
}); | |
return t; | |
}; | |
var generateOptionElements = function(list) { | |
var parentElement = document.createElement('div'); | |
list.forEach(function(e) { | |
var n = document.createElement('p'); | |
n.textContent = '<option value="' + e + '">' + e + '</option>'; | |
parentElement.appendChild(n); | |
}); | |
return parentElement; | |
}; | |
document.querySelector('button').addEventListener('click', function() { | |
var t = extractArray(); | |
var retElm = document.querySelector('.ret'); | |
retElm.textContent = ''; | |
retElm.appendChild(generateOptionElements(t)); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment