3

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

4
  • may you create an array with data before to have clean data. See api.jquery.com/jquery.inarray Commented Sep 29, 2014 at 8:10
  • Very first time its working fine(2nd time only its appending some duplicate data's). Commented Sep 29, 2014 at 8:14
  • i tried with breakpoint. the loop is executing more than 9 times instead of 3 times :( Commented Sep 29, 2014 at 8:15
  • btw is this a typo activityconunt ? shouldnt this be activitycount ? and updated my answere with a faster and tested solution. Its way less code the all other solutions. Actually only one line ^^ Commented Sep 29, 2014 at 8:26

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

hi Dwza its working fine. but im getting one undefined value also in the list :|
1

Use an array to store the data and check the new value with it:

$(function () {
  var items = [];
  var $select = $('select');
  var $input = $('input');
  var $button = $('button');

  // fetch current data
  $select.find('option').each(function () {
    items.push($(this).text());
  });

  $button.on('click', function () {
    var value = $input.val();
    var exists = ($.inArray(value, items) != -1);

    if (! exists) {
      items.push(value);
      $('<option></option').text(value).prependTo($select);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" />
<button>Add</button>
<br />
<select style="width: 300px;">    
    <option>Hello</option>
</select>

Comments

1

Solution for preventing duplicate values and undefined value in the list

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.