Results 1 to 2 of 2

Thread: display an alert box when an existing option is added to dropdown from a textbox

  1. #1
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default display an alert box when an existing option is added to dropdown from a textbox

    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

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •