Current location: Hot Scripts Forums » General Web Coding » JavaScript » Preventing dupes in javascript array?


Preventing dupes in javascript array?

Reply
  #1 (permalink)  
Old 11-12-09, 09:50 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Preventing dupes in javascript array?

I have a form that lets people copy items from one select list to another, but I want to prevent them from putting duplicate items into the second list. In other words, they can pick as many items from the first list as they want, but they can't pick and add the same one twice.

In the code below, what would be the best way to prevent adding duplicate items to the "destList.options" array?

HTML Code:
// Populate  destination with  items from new array
for ( var j = 0; j < newDestList.length; j++ ){

    if ( newDestList[ j ] != null ){

	      destList.options[ j ] = newDestList[ j ];

    }

}
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-12-09, 12:47 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Why not remove the item from the first list as they are added to the second?
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 11-12-09, 01:27 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by TwoD View Post
Why not remove the item from the first list as they are added to the second?
My bad, I should have explained this in the original post. In this particular case I need to leave all of the elements in the original array.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-12-09, 07:30 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
I have this mostly sorted, but I'm now trying to figure out how to add an 'onclick()' event to a dynamically created element:

var y=document.createElement('option');
y.value=tmp1;
y.text=tmp;
y.onclick = moveinid();

I've tried numerous variations on the line in bold, using setAttribute, attachEvent, etc etc and still haven't hit on it. How does one attach an onclick event in this circumstance?
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 11-13-09, 06:17 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Remove the parenthesis. You're calling the function and assigning its return value to onclick, instead of assigning it the function reference itself.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 11-13-09, 07:23 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by TwoD View Post
Remove the parenthesis. You're calling the function and assigning its return value to onclick, instead of assigning it the function reference itself.
Actually, I tried that earlier but couldn't get that to work either. I also tried 'addListener', and a couple of other variations with no luck.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 11-14-09, 10:34 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
HTML Code:
<script type="text/javascript">
document.write('<span id="test">Test</span>');
t=document.getElementById('test');
t.setAttribute('onclick',bork);  /* Set onclick attribute */
function bork(e)
{
alert('Bork');
}
</script>
As far as the duplicate issue - options:

Create an array that lists the items in the first select statement, as well as a flag to indicate whether the user selected them. If the user already selected the item, disregard reselections.

-- or --

Use the sort JavaScript sort() Method to sort the target array. That should make the search process quicker.

-- or --

Flatten the target array with JavaScript join() Method and search the resultant string.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 11-15-09, 08:09 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
HTML Code:
<script type="text/javascript">
document.write('<span id="test">Test</span>');
t=document.getElementById('test');
t.setAttribute('onclick',bork);  /* Set onclick attribute */
function bork(e)
{
alert('Bork');
}
</script>
Hmmmm, I tried the code above a couple of ways but couldn't get it to work. I think it may be due to the fact that I'm dynamically building the list via javascript, but I really don't know yet.

On the dupe-prevention, I did resort to just searching the list each time and rejecting the element addition if it was already there.

Finally (or maybe not, lol) I swear that IE is the most retarded browser in the Universe. Now, maybe there's another Universe where IE is not the most retarded browser, but that remains to be seen. I say this for one simple reason:

IE just doesn't support event handlers from the option element. In other words, this code here won't work:

HTML Code:
<form>
<select>
<option onclick="doSomething()">Option Number 1</option.
</select>
</form>
It'll work in every browser that I know of except IE. The onclick only works in IE when it's used on the <select> tag. Yeah, there are workarounds to handle both cases, but I'm so sick of having to code extra junk for IE that I want to vomit. IE simply does not follow the HTML specifications:

Forms in HTML documents
http://www.w3.org/TR/html401/interact/forms.html#h-17.6

It says right there that the <OPTION> part of the <SELECT> element has an onclick event handler available. Not "maybe", not "optionally", not "if Microsoft feels like it", the spec says it will contain this event as an intrinsic property.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 11-16-09, 07:05 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
You might want to use onchange/onclick on the select.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove value from array Nikas PHP 1 05-20-09 04:20 AM
Multi-Dimensional Array Help Nikas PHP 17 05-04-09 03:10 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
how to pass php array to javascript jaishalg PHP 2 12-23-05 04:28 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 02:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.