Kenny Ong
Kenny Ong

Reputation: 401

jquery remove textarea duplicate line

i has a problem with textarea find duplicate.

<textarea name="listnumber" >
12345678
12345674
12345678
12345672
12345678
</textarea>

and i auto remove duplicate when change using jquery. result will be like this

<textarea name="listnumber" >
12345678
12345674
12345672
</textarea>

Question : how to remove duplicated entries in the text area. as expected result shown above

EDIT

i got an textarea with name="listnumber"

My Source Code:

   <script type="text/javascript">
<!--
 $(document).ready(function() {
    $("textarea#listnumber").bind('input propertychange keyup cut paste',function(){
        text = $("textarea#listnumber").val()
        lines = $.unique(text.split(/\r|\r\n|\n/))
        count = lines.length
        $('input#totalnumber').val(count)
    });
});
-->
</script>
<form action="index.php" method="post">
    <ul>
        <li><label>List Of Number</label><textarea name="listnumber" id="listnumber" ></textarea></li>
        <li><label>Total Number</label><input type="text" name="totalnumber" readonly="readonly" /></li>
    </ul>
    <a onclick="submit();" href="#" >Send List</a>
</form>

and i want jquery to remove the duplicate inside the textarea with using $('textarea').change(function(){} but i dont know how to do this. any one please help.

Upvotes: 0

Views: 3404

Answers (2)

Milan Mendpara
Milan Mendpara

Reputation: 3131

this might be helpful to you : http://jsfiddle.net/Milian/Y3WLk/

$("#anchor").click(function(){
    var arr = $("#txtArea").val().split("\n");

    var arrDistinct = new Array();
     $(arr).each(function(index, item) {
         if ($.inArray(item, arrDistinct) == -1)
                    arrDistinct.push(item);
     });
    alert(arrDistinct);

});

Upvotes: 1

Zim84
Zim84

Reputation: 3497

Try splitting your textarea-text into an array: Javascript: Convert textarea into an array

And then find duplicates Easiest way to find duplicate values in a JavaScript array

Btw: time used to do this research: under a minute (incl. writing this post).

Upvotes: 3

Related Questions