How to select Muntliple input to remove from the list
Hi,
I have the code , which add the data from input filed to list-box.Now am able to remove individual input's.
I want to have option multiple select to remove the data from list box .
I am not able to process the data whichever present in select tag.........
CODE:
<form id="frm" action="" method="post">
Select list:<br/>
<select name="sel_list" id="sel_list" size="2" onchange="adOption.selOpt(this.value, 'optval')"></select><br/><br/>
Add an option: <input type="text" name="optval" id="optval" /><br /><br/>
<input type="button" id="addopt" name="addopt" value="Add Option" onclick="adOption.addOption('sel_list', 'optval');" />
<input type="button" id="del_opt" name="del_opt" value="Delete Option" onclick="adOption.delOption('sel_list', 'optval');" />
</form>
<script type="text/javascript"><!--
var adOption = new Object();
adOption.checkList = function(list, optval) {
var re = 0;
var opts = document.getElementById(list).getElementsByTagName('option');
for(var i=0; i<opts.length; i++) {
if(opts[i].value == document.getElementById(optval).value) {
re = 1;
break;
}
}
return re;
};
adOption.addOption = function(list, optval) {
var opt_val = document.getElementById(optval).value;
if(opt_val.length > 0) {
if(this.checkList(list, optval) == 0) {
var myoption = document.createElement('option');
myoption.value = opt_val;
myoption.innerHTML = opt_val;
document.getElementById(list).insertBefore(myoption, document.getElementById(list).firstChild);
document.getElementById(optval).value = '';
}
else alert('The value "'+opt_val+'" already added');
}
else alert('Add a value for option');
};
adOption.delOption = function(list, optval) {
var opt_val = document.getElementById(optval).value;
if(this.checkList(list, optval) == 1) {
var opts = document.getElementById(list).getElementsByTagName('option');
for(var i=0; i<opts.length; i++) {
if(opts[i].value == opt_val) {
document.getElementById(list).removeChild(opts[i]);
break;
}
}
}
else alert('The value "'+opt_val+'" not exist');
}
adOption.selOpt = function(opt, txtbox) { document.getElementById(txtbox).value = opt; }
--></script>
How can I do this ....?
Please Help
Thank You
Pervez
See, for example, this page Changing Select element content on the fly to use the Option() constructor which is easier to use as document.createElement('option').
To remove multiple options, make a loop and remove the selected options, as single option, with index reverse order (to avoid to change the other indexes).
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks