1

I have a few select menus that include blank options. When both are blank (usually on the first page load), I would like to show some hidden div.

This is what I have:

$('.variant_options select').each(function() { 
    if ($(this).attr('value') === '') {
        // some code here to show hidden div
        console.log("No options chosen");
    }
});

This doesn't seem to work.

Edit 1

For what it's worth, I have tried something like this:

if (!$(this).attr('value'))

And that has seemed to work, but it breaks functionality elsewhere.

3
  • 1
    if ($(this).val() === '') {? Commented Mar 26, 2013 at 10:36
  • if ($(this).prop('value') === '') or just if (!this.value) Commented Mar 26, 2013 at 10:39
  • What's with all the downvotes? Commented Mar 26, 2013 at 10:48

5 Answers 5

5

<select> elements don't have a value attribute, so you need to use .val() on the element to find out if the currently selected option is empty.

if ($(this).val() === '') {
    // value of select box is empty
}

this.value === '' should also work

To check whether no options are selected:

if (this.selectedIndex == 0) {
    // no option is selected
}
Sign up to request clarification or add additional context in comments.

2 Comments

I like where you are going with this.selectedIndex == -1 but that doesn't work for me. That's actually the functionality I am looking for - if no select box value has been selected, then show some hidden div.
Or more specifically...on the first page load when it defaults to blank on both select menus, the top solution you have provided here works properly. However, if I chose a value for 1 (or both) of the options, then I go back to blank for both select menus, this no longer works. How can I get it to do this once it has returned to a 'blank' state - even after a value has been selected before? I suspect that this is where your selectedIndex value was going...which is why I would love to see more of it :)
1

You can do so by using the following:

if($(this).val() === '') {
  // value is empty
}

I believe also the following too:

if(!$(this).prop('value')) { 
  // It's empty 
}

5 Comments

He does use attr and it doesn't work. What do you mean by or, when to use what?
@Bergi That was edited in to the question after I answered, I'll update.
Both of these work for 95% of the cases, i.e. on the first page load when it defaults to blank on both select menus, it does this properly. However, if I chose a value for 1 of the options, then I go back to blank for both select menus, this no longer works. How can I get it to do this once it has returned to a 'blank' state - even after a value has been selected before?
Hmm.. what 'event' is this wrapped inside? (e.g. click, on etc.) do you have a jsFiddle by any chance?
Nvm....I got it. Thanks though! I updated Jack's answer to reflect the correct version.
1

You can simply do this:

$('.variant_options select').each(function () {
    if ($.trim($(this).val()) === '') {
        // some code here...
    }
});

3 Comments

In case, if you have spaces in options values
Oh...well I am only interested in the no-value case here...i.e. on the first load of the page, when the select options are blank (or when someone chooses the blank options).
Yes....that's interesting. A lot more code than just this.selectedIndex == 0, but that does achieve what I am looking for.
0

jQuery can check for value by using $(this).val()

So you would do if ($(this).val === '')`

If you wanted to check for some other attribute, like href or src, you could do

if ($(this).attr('href') === ''

Comments

0

In case if you have spaces, use this trick:

if ($.trim($(this).val()) === '') { ...

1 Comment

No spaces, because the values are in a pre-defined dropdown menu. I just want to detect if any of the select options have been selected (i.e. if the values are all undefined or blank, then the answer should be no and should show some hidden div).

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.