javascript - jQuery: How to search the user defined array? -


the user can define how many <select> choose , when <select> on change event triggered, value of <select> push array. basically, value , length of array depending on user. now, want if user selected specific value on other <select> , tries select on other <select>, prompt message item selected. well, item name of existing value select. don't know how it. here codes adding <select>.

var = 1;  $('#add').click(function() {    var cnt = $('#append').val();    (var = 0; < cnt; i++) {      a++;      $('#contain').append('<select class="sel" name="' + + '"></select><button id="remove" name="' + + '">x</button><br name="' + + '"/>');    }    var selval = $('#contain select').val();    var optarr = [];    $('#contain #sel1 option').each(function() {      var opt = $(this).val();      if (opt != selval && opt != 'null') {        optarr.push(opt);      }    });    var optjoin = optarr.join(',');    var optlen = optarr.length;    var optsplit = optjoin.split(',');    (var x = 0; x < optlen; x++) {      var optappend = '<option value="' + optsplit[x] + '">' + optsplit[x] + '</option>';      $('#contain select.sel[name="' + + '"]').append(optappend);    }  });  $(document).on('click', '#contain #remove', function() {    $('#contain select.sel, #contain #remove, #contain br').remove('[name="' + + '"]');    a--;  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type='text' id='append' value='1' />  <button id='add'>+</button>  <br/>  <br/>  <div id='contain'>    <select id='sel1' class='sel1'>      <option value='val1'>val1</option>      <option value='val2'>val2</option>      <option value='val3'>val3</option>      <option value='val4'>val4</option>      <option value='val1'>val5</option>      <option value='val2'>val6</option>      <option value='val3'>val7</option>      <option value='val4'>val8</option>      <option value='val3'>val9</option>      <option value='val4'>val10</option>    </select>    <br/>  </div>

you can use filter() method without need jquery.

run snippet created below.

the important piece of code when var filtered gets assigned. created 2 tests. see below comments // first test , // second test.

you can check if filtered.length === 0 see if target value in array demonstrated in inarray() function.

var array = ['val1', 'val4', 'val2'],      filtered;    function inarray(outputid) {      var inarraytext = filtered.length === 0 ? 'no' : 'yes';      document.getelementbyid(outputid).innerhtml = inarraytext;  }    // first test  filtered = array.filter(function (val) {      return val === 'val5';  });  inarray('result1');    // second test  filtered = array.filter(function (val) {      return val === 'val2';  });  inarray('result2');
<div>is 'val5' in ['val1', 'val4', 'val2']? <span id="result1"></span></div>  <div>is 'val2' in ['val1', 'val4', 'val2']? <span id="result2"></span></div>

if need use jquery, user86745458 suggested, can use jquery's inarray() so:

var indexofvalinarray = $.inarray(val, array);

this gets index val located in array or -1 if doesn't exist.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -