The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Checking for duplicate entries in select box
Discuss Checking for duplicate entries in select box in the JavaScript Development forum on Dev Shed. Checking for duplicate entries in select box JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 28th, 2013, 04:01 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 23
Time spent in forums: 2 h 20 m 59 sec
Reputation 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
|

January 28th, 2013, 07:02 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
|
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.
|

January 28th, 2013, 10:31 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 23
Time spent in forums: 2 h 20 m 59 sec
Reputation 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
|

January 29th, 2013, 03:17 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 1 h 5 m 43 sec
Reputation Power: 0
|
|
Quote: | 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
|

January 29th, 2013, 10:19 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 23
Time spent in forums: 2 h 20 m 59 sec
Reputation Power: 0
|
|
|
Thanks very much!
Works like a charm.
That did the trick.
Cheers!
Jay
|

January 29th, 2013, 09:51 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 1 h 5 m 43 sec
Reputation Power: 0
|
|
Hi,
Its ok.
Enjoy
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|