0

Hi every body, i'm a newbie in javascript, so i need all your help

i have a javascript like this:

<script type="text/javascript">

    function validate(target, e){

        var id = e.dataTransfer.getData('div');
        var clone = document.getElementById(id).cloneNode(true);
        var allValues = []; 
        for (i=0; i<clone.length; i++)
            {
             if (clone[i].value != "")
                {
                 allValues[i] = clone[i].value.toLowerCase();
                }
            }
        allValues.sort();       
        var uniqueValues = [];      
        var idx = 0;
        var prevValue = allValues[0];
        var currValue = allValues[0];
        for (i=0; i<allValues.length; i++)
            { 
             currValue = allValues[i];
             if (currValue != prevValue){idx++;}
             uniqueValues[idx] = currValue;
             prevValue = currValue;
            }
        if (uniqueValues.length != allValues.length)
            {

              e.preventDefault();
              clone.parentNode.removeChild(clone);
             alert('Cannot submit duplicate entries');
             return false;
            }
        else    {
             alert(clone.id);
             e.preventDefault();
             target.appendChild(clone); 
             return true;
            }
    }

</script>

So i'm trying to do ondrop function(validate), so when i drop something for the first time,the validate function will run and drop the div value. But for the second time and so on, if i dropped the same div value i want it to alert duplicate and remove that div element, but i'm unsuccessfull in detecting the duplicate div.Can anyonr tell me whats wrong with my code?
Firstly Thanks for the help

2
Contributors
1
Reply
14
Views
4 Years
Discussion Span
Last Post by charles nitro
1

Your best bet is to check for duplicates before you print them to the list. If they are always sorted (as it appears in your example), then you can easily do this just by keeping track of what was the last value you used. Make sure the current value does not match, then display it and assign it as the new last value

Votes + Comments
thx i will try your suggestion
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.