Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i prevent duplicate entry in java script while appending data into #div

PFB java script code..

the thing is im getting alert for duplicate entry. how can avoid the repeated data?

Var   activityconunt =0;     
  if (activityconunt !== data.iRoundId) {
        alert("duplicate");
        $("#selectRound_Type").append("<option name='round' id=" + data.iRoundId + ">" + data.strRoundName + "</option>");             
        }

my output my output

like image 401
Arul Sidthan Avatar asked Jan 20 '26 11:01

Arul Sidthan


1 Answers

Solution one:

Take your data and build a clean array before. Using http://api.jquery.com/jquery.inarray/

Solution two:

Check your existing options for containing values

if($("option:contains('" + data.strRoundName + "')").length == 0)
    $("#selectRound_Type").append("<option name='round' id=" + data.iRoundId + ">" + data.strRoundName + "</option>");

this should do it as well and is a shorter code

also see Fiddle

like image 135
Dwza Avatar answered Jan 22 '26 00:01

Dwza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!