#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    27
    Rep Power
    0

    Checking for duplicate entries in select box


    Hello,

    I need your help,

    The following function below, will dynamically add a new option value into a selectbox. Great feature, but it does not account and check for duplicate entries before adding new options into the select box. How can the code be modified such that it will alert the user that a duplicate entry has been found and to abort adding the same option value:

    Code:
    function addref() {
    
    var value = document.getElementById('refdocs').value
    
        if (value != "") {
    
            var select = document.getElementById('refdocs_list');
    
            var option = document.createElement('option');
    
            option.text = value
    
            select.add(option,select.option)
    
            select.selectedIndex = select.options.length - 1;
        }//end of if
    
    }//end of function
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,128
    Rep Power
    4304
    You would need to loop through all of the existing options and check if you had a match. The loop would be added before your if condition and if a match was found, so you would notify the user somehow, e.g. with alert(), and then execute a "return" statement.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    27
    Rep Power
    0
    Thanks very much for the post. But how on earth would you go about the process that you described. Sorry, I am a complete newbie still.

    Jay
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by Jason_Kelly
    Thanks very much for the post. But how on earth would you go about the process that you described. Sorry, I am a complete newbie still.

    Jay
    Hi,

    try the below code :-

    Code:
    <!DOCTYPE html>
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
    <script type = "text/javascript">
    function addref() 
    {
    	var value = document.getElementById('refdocs').value
        if (value != "") 
        {
            var select = document.getElementById('refdocs_list');
            var option = document.createElement('option');
    	var flag = 0;
    	for(var i = 0; i < select.length; i++)
    	{
    		if(select[i].value == value)
    		{	
    			flag = 1;
    		}
    	}
    	if(flag == 1)
    	{
    		alert('Duplicate value, Please enter a new one.');
    	}
    	else
    	{
    		option.text = value;
    		select.add(option,select.option);
    		select.selectedIndex = select.options.length - 1;
    	}
    	
        }
    }
    </script>
    </HEAD>
    <BODY>
    <div>
    	<input type = "text" name = "refdocs" id = "refdocs" style = "width:80px;"/>
    	<input type = "button" name = "addValues" id = "addValues" value = "AddValues" onClick = "addref();"/>
    	</br>
    	<select id = "refdocs_list">
    	<option value = "1">1</option>
    	<option value = "2">2</option>
    	</select>
    </div>
    </BODY>
    </HTML>
    Let me know if you get any problem.
    Last edited by Kravvitz; January 29th, 2013 at 03:35 AM. Reason: changed the doctype to a current one and added code tags
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    27
    Rep Power
    0
    Thanks very much!

    Works like a charm.

    That did the trick.

    Cheers!

    Jay
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Hi,

    Its ok.

    Enjoy

IMN logo majestic logo threadwatch logo seochat tools logo