4 Replies - 6660 Views - Last Post: 21 November 2015 - 08:04 AM

#1 StealthRT   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 29-September 08

Check for checked/unchecked and value SumoSelect

Posted 19 November 2015 - 08:49 AM

Hey all I have a working fiddle HERE that I need some assistance with.

I am trying to see if the current selected item in the dropdown is either checked or uncheck and also gets its value for each.

Currently it's only showing the value if its true and nothing if its false. Also, the other issue is that if more than one is already selected it defaults to the first one in the list even if the selected one is after the current selected one. I figured it would only see the one being selected at the time it fired off onchange?

Current code is this:
$('#boutique_drop').SumoSelect({triggerChangeCombined: false});

$("#boutique_drop").on('change', function() {
   if ($('option:selected', this).is(':checked')) {
      alert('is checked: ' + $(this).val());    
   } else {
      alert('is not checked' + $(this).val());
   }
})


Any help would be great!

This post has been edited by StealthRT: 19 November 2015 - 08:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Check for checked/unchecked and value SumoSelect

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6833
  • View blog
  • Posts: 28,342
  • Joined: 12-December 12

Re: Check for checked/unchecked and value SumoSelect

Posted 19 November 2015 - 09:45 AM

if ($('option:selected', this).is(':checked')) {

Using :selected it is only looking at those items which are checked, so the second alert will never appear. So,
if ($('option', this).is(':checked')) {

but this will only work with the first checked, or unchecked, item. You need to use each() to produce a message for every option.
Was This Post Helpful? 0
  • +
  • -

#3 StealthRT   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 29-September 08

Re: Check for checked/unchecked and value SumoSelect

Posted 20 November 2015 - 01:01 PM

Even doing:
alert($(this).val());


before the IF statement shows null as the value....
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6833
  • View blog
  • Posts: 28,342
  • Joined: 12-December 12

Re: Check for checked/unchecked and value SumoSelect

Posted 20 November 2015 - 01:32 PM

SumoSelect will be changing things. You need to study it more to understand how it works. Presumably it offers an API/methods that you can use to discover what has been selected.

This post has been edited by andrewsw: 20 November 2015 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 blankwavercade   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 123
  • Joined: 13-December 11

Re: Check for checked/unchecked and value SumoSelect

Posted 21 November 2015 - 08:04 AM

This code snippet is working exactly as it should. (In the sense of how it is setup). Take a look at your fiddle, select a few options, you'll notice that each alert box shows "Is Checked: <val1>,<val2>,<val3>..." This is correct on how you have it setup. Using
$(this).val()
will get each of the values in the select box. The false logic also works. Try deselecting all of the values that you have selected and it will alert with the false alert box. The reason this is happening the way it is, is because at that point the jquery this object has no value and returns null. Before while you are deselecting an option the jquery this object still has values. Your logic is setup to check if the this object has a truthy value.

I don't know your use case for having a select box with checkboxes but ideally this is correct. For instance, if you're submitting a form via ajax you'll be serializing your data. for this select box the end result of the serialization would look something like
key:val1,val2,val3,val4....
which you would handle the data however you want on the backend. When you have no options selected then it will return null and you won't be worrying about.

Can you explain your intended use of this? It may be easier to give a better answer with a more clear use case and see how and what you want to do other than just alerting the val.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1