PDA

View Full Version : display an alert box when an existing option is added to dropdown from a textbox



shravanti88
03-20-2010, 04:23 AM
Hi,

I have a form that includes dropdown box, text box and add button.when a value is entered in the text box and the add button is clicked, it will added to the dropdown box. I need to create an alert message displaying "the value already exists" when an existing option is entered in the textbox.
The code i have created is as follows.Now i need to include a statement within the function to display an alert message.

[code]<html>
<script type= "text/javascript" language="javascript">
function addition()
{
var textb= document.getElementById("txtCombo");
var combo= document.getElementById("combo");
var option= document.createElement("option");
option.text= textb.value;
option.value= textb.value;
combo.add(option);
}
</script>
<body>
<form>
location <select name="combo" id="combo" >
<option>ooty
<option>kodai
<option>chennai
</select>
<input type="text" name="txtCombo" id="txtCombo">
<input type="button" value="add" onclick="addition()">
</form>
</body>
</html>[code]

Thanks,

Regards,
Shravs

vwphillips
03-20-2010, 10:48 AM
<html>
<script type= "text/javascript" >
function addition(){
var text= document.getElementById("txtCombo").value;
var combo= document.getElementById("combo");
if (!this.save){
this.save=[''];
for (var z0=0;z0<combo.options.length;z0++){
this.save.push(combo.options[z0].value)
}
}
if (text.replace(/\s/g,'')==''){
alert('Blank');
return;
}
for (var z0=0;z0<this.save.length;z0++){
if (this.save[z0]==text){
alert('Duplicate');
return;
}
}
this.save.push(text);
var option= document.createElement("option");
option.text= text;
option.value= text;
combo.add(option);
}
</script>
<body>
<form>
location <select name="combo" id="combo" >
<option>ooty
<option>kodai
<option>chennai
</select>
<input type="text" name="txtCombo" id="txtCombo">
<input type="button" value="add" onclick="addition()">
</form>
</form>
</body>
</html>