PDA

View Full Version : Selectboxscript: Add an errormessage?


viktor
03-13-2003, 08:35 AM
The code below moves options into a selectbox, provided that the value of the option isn't already in the selectbox. My question is: How can keep this code but add an errormessage when user tries to add a value which is already in the selectbox? Thanks, hope someone can help! /Viktor

THE FUNCTION:
function addOpt(optValue,optText,frm_name,lstBox_name){
var Box = document.forms[frm_name].elements[lstBox_name];
for(var i=0;i<Box.options.length;i++) if(Box.options[i].value == optValue) return;
var newOption = new Option(optText,optValue);
Box.options[Box.options.length] = newOption;
}

THE LINK THAT CALLS THE FUNCTION:
onclick="javascript:addOpt(optionvalue1,'optiontext1','myform','myformobject')"

arnyinc
03-13-2003, 01:36 PM
looks like changing this statement would do it. Hard to tell with only the function and function call though.

if(Box.options[i].value == optValue) return;

to

if(Box.options[i].value == optValue){
alert('error');
return;
}

viktor
03-13-2003, 02:39 PM
Many thanks arnyinc! Exactly what I was looking for! Best Regards/Viktor