I'm trying to reload the options list of a select with JavaScript and JQuery, but I need to preserve previous chosen values.
This is my code:
var temp = $('#SelectName').chosen().val();
select = document.getElementById('SelectName');
if (select.options.length > 0) {
$('#SelectName').find('option').remove();
}
for (var i = 0; i < result.length; i++) {
var option = document.createElement('option');
option.value = result[i].myIdVariable;
option.text = result[i].myTextVariable;
select.appendChild(option);
}
$('#SelectName').chosen(temp);
$('#SelectName').trigger('chosen:updated');
With this code I loose all the chosen values.
I solved with this code:
var temp = $('#SelectName').val();
select = document.getElementById('SelectName');
if (select.options.length > 0) {
$('#SelectName').find('option').remove();
}
for (var i = 0; i < result.length; i++) {
var option = document.createElement('option');
option.value = result[i].myIdVariable;
option.text = result[i].myTextVariable;
select.appendChild(option);
}
$('#SelectName').val(temp);
$('#SelectName').trigger('chosen:updated');
The difference is that now I use:
var temp = $('#SelectName').val();
and
$('#SelectName').val(temp);
previously:
var temp = $('#SelectName').chosen().val();
and
$('#SelectName').chosen(temp);
I have a select field as follows:
<select id="field">
I add options based on values found in another div (OtherDiv) on my page like this:
window.onload = function onload()
{
var OtherDiv = document.getElementById('OtherDiv').innerHTML;
var result = OtherDiv.match(/SomeRegex/gi);
var select = document.getElementById("field");
for(var i = 0; i < result.length; i++)
{
var option = document.createElement("option");
option.value = i+1;
option.innerHTML = result[i];
select.add(option);
}
}
However, I would like to set up some alternative value for the field to show, if there are no matches to the regex. How would I best achieve that?
Use if condition
window.onload = function onload()
{
var OtherDiv = document.getElementById('OtherDiv').innerHTML;
var result = OtherDiv.match(/SomeRegex/gi);
var select = document.getElementById("field");
if(result.length){
for(var i = 0; i < result.length; i++)
{
var option = document.createElement("option");
option.value = i+1;
option.innerHTML = result[i];
select.add(option);
}
}else{
//add alternative options here
var option = document.createElement("option");
option.innerHTML = "No records found"; //whatever text you want to show
select.add(option);
}
}
I want to fill a select with and array values. I don't see why my code doesnt work. I am a beginner.This is my script
var mes = new Array();
for(i = 0; i < 12; i++){
mes[i] = i;
}
var sel = document.getElementById('mes');
for(var i = 0; i < mes.length; i++) {
var opt = document.createElement('option');
opt.innerHTML = mes[i];
opt.value = mes[i];
sel.appendChild(opt);
}
And this is the html select:
<select name="mes" id="mes"></select>
I have a value in textbox as '203' in that '0' should be increment every time as [1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,.......Z] so next increment value should be '213,223,233,243,253,263,273,283,293,2A3,2B3,2C3,2D3,2E3....'
So I created a dropdown box 'revision' where the values as[1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,.......Z]
using sustr I strip the '0' position and I got value in variable 'value1'.
var value1=item.substr(0,1)
var arr = document.getElementsByName('revision')
var x = arr[0].selectedIndex
How do I compare the value1 in arr[0].length and increment to next as of dropdown data?
Try
var currentIndex = 0;
function createOption(){
var select = document.getElementById("revision");
for(var i = 1; i <= 9 ; i++){
var opt = document.createElement("option");
opt.value = i;
opt.innerHTML = i;
select.appendChild(opt);
}
for(var i = 65; i <= 90; i++){
var opt = document.createElement("option");
opt.value = String.fromCharCode(i);
opt.innerHTML = String.fromCharCode(i);
select.appendChild(opt);
}
}
createOption();
document.getElementById("btn").onclick = function(){
currentIndex ++;
var txt = document.getElementById("txt");
var select = document.getElementById("revision");
var lastChar = txt.value.substr(txt.value.length - 1, 1);
var firstChar = txt.value.substr(0, 1);
select.value = select.childNodes[currentIndex].value;
txt.value = firstChar + select.childNodes[currentIndex].value + lastChar;
};
Here's DEMO
My goal: i need to read txt file using fso - activeX then separate text by ; and put each separated word in select - option.
so far i got this but aint working
<script>
function readAll() {
var fso = new AcitveXObject("Scripting.FileSystemObject");
var txtFile = fso.OpenTextFile("kategorije.txt", 1, false, 0);
var fText = txtFile.ReadAll();
txtFile.Close();
}
var array = fText.split(";");
var sel = document.getElementById("dropdown2");
for (var i = 0; i < dropdown2.length; i++) {
var opt = document.createElement("option");
opt.innerHTML = fText[i];
opt.value = fText[i];
sel.appendChild[opt];
}
</script>
function readAll() {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var txtFile = fso.OpenTextFile("kategorije.txt", 1, false, 0);
var fText = txtFile.ReadAll();
txtFile.Close();
fso = null
var array = fText.split("\r\n");
var sel = document.getElementById("dropdown2");
for (var i = 0; i < array.length; i++) {
var opt = document.createElement("option");
opt.innerHTML = array[i];
opt.value = array[i];
sel.appendChild(opt);
}
}
my code works now and it look like this