0

I have a input with select2 like below

<select class="form-control" name="location" multiple="multiple" id="location"></select>

So I want user to input / type the location, and user can typing it multiple times and each location separated by press enter button

I want to make system, that if user have 10 locations needed to input, when location number 7 input have same value with location number 3 user typed before, it shows alert to make user aware.

Right now, if user typing location number 7 input input have same value with location number 3 user typed before, the data location number 7 is just disappear.

So I want to get value from location selected, but I'm confused how to get data that just disappear like the case above. I tried to make if condition, if value of location is not increased, it shows an alert javascript, but it's not showing an alert

Here's the code

 $('.select2').on('change', function() {
    var test = $("#location").val(); 
    console.log(test) // shows value in array like ["location 1","location 2",..]
    var a = test.length
    if(a != a+1)
    {
        alert("you already input that location");
    }
 })

Any clue would be help me, thank you

Ikram Shabri
  • 557
  • 1
  • 7
  • 19
  • What if you made the event listener function take a parameter of `e` and then try to alert `e.target.innerText` – Zak Apr 02 '21 at 13:51
  • 1
    if you think about it, this if condition will always be true `if(a != a+1)` you rather want to check the arrays values sth like: https://stackoverflow.com/questions/49215358/checking-for-duplicate-strings-in-javascript-array – john Smith Apr 02 '21 at 14:12
  • Hi @Zak, how to do that ? – Ikram Shabri Apr 02 '21 at 14:25
  • Hi @johnSmith, still have a problem, when I do `console.log(test)`, if location number `7` is same with location number `3`, the location number `7` is not entering an array – Ikram Shabri Apr 02 '21 at 14:27
  • i guess its already built in by select2 that you can not have same input twice? so its working already as you expect? – john Smith Apr 02 '21 at 14:39
  • `a` will never equal `a+1` – freedomn-m Apr 02 '21 at 14:40
  • Note that a `select2` pills picker is just a wrapper over a ``. Try without the `select2` wrapper in the first instance and you'll see that `$("select").val()` always picks the values you have chosen, in the order they appear in the `select`. – freedomn-m Apr 02 '21 at 14:43
  • HI @johnSmith yes, it is – Ikram Shabri Apr 03 '21 at 04:00
  • hi @freedomn-m I've tried this code like you're suggest `$('.select2').on('change', function(){ var v = $("select").val; console.log(v) })`, but I just have this code in `console.log` `f(t){var e,i,n,a=this[0];return arguments.length?(n=m(t),this.each((function(i){var a;1===this.nodeType&&(null==(a=n?t.call(this,i,x(this).val()):t)?a="":"number"==typeof ...` – Ikram Shabri Apr 03 '21 at 04:05
  • I don't think I would ever suggest `.val` - try `.val()` - and learn to recognise when you've output a function definition into the console so need to and `()`. And I was just explaining what the code you already have does, not suggesting you change the selector to `select` - that's just easier to type and more general than your specific id. – freedomn-m Apr 03 '21 at 09:05

0 Answers0