44

I have a select box that is being populated dynamically from a database. As of right now the population of this check box is working flawlessly.

I have added functionality that consists of a button which on click calls isSelextBoxEmpty. The job of this function is to know whether this particular check box has been populated or not; if it has not then it will simply do nothing.

My problem is in determining whether this select box is empty or not.

Here is a very simplified example of what I am dealing with:

<li>
    <label for="fruit_name">Fruit</label>
    <select name="some_fruit" id="fruit_name" onclick="populate_box('fruit', this);">
    </select>
</li>

My function, which is called from a separate button, looks like this:

function isSelextBoxEmpty(selectBoxId) {
    var selected_value = $('#fruit_name');

    /* More options... still testing the proper way:
    var selected_value = $('#fruit_name').text;
    var selected_value = $('#fruit_name').value;
    var selected_value = $('#fruit_name').length;
    var selected_value = $('#fruit_name option:selected', this);
    var selected_value = document.getElementById('fruit_name');
    var selected_value = document.getElementById('fruit_name').length;
    var selected_value = document.getElementById('fruit_name').value;
    var selected_value = document.getElementById('fruit_name').innerHTML;
    */

    if (selected_value) {
        alert("NOT null, value: " + selected_value);
        // do something
    }
}

Don't worry about what this does and how it does it. Right now what matters to me is that I can't check whether or not the checkbox is empty, I am just not sure how to go about it. I have read a lot through forums and documentation but there are many implications in doing this since it depends on the implementation itself.

For instance using document.getElementById(...)... will not necessarily return false and it depends on how you use it. Also using $("#someID")... in jQuery may or may not produce the desired results. I have already tried many different times as you can see in the commented lines, all of which can be evaluated in the if(...) statement.

How can this be done?

starball
  • 20,030
  • 7
  • 43
  • 238
AGE
  • 3,752
  • 3
  • 38
  • 60

4 Answers4

108

To check whether select box has any values:

if( $('#fruit_name').has('option').length > 0 ) {

To check whether selected value is empty:

if( !$('#fruit_name').val() ) { 
Engineer
  • 47,849
  • 12
  • 88
  • 91
  • 1
    I awarded the answer to you because you got it right before Claudio, but actually Claudio also got it right just a few seconds after you did... THANKS both of you! I wish I could split the reward. – AGE Jun 14 '12 at 19:17
  • The latter code is not correct. Infact, if you try it with a select box you receive null as the val(), but @if( !$('#fruit_name').val() )@ returns TRUE. – sentenza May 29 '15 at 07:59
  • 1
    Well I'm trying to take away my downvote because this is correct, but stackoverflow won't let me. Pretty lame. Sorry for the downvote, it should be an upvote. – Greg Blass Jan 15 '16 at 16:09
  • @Engineer Thanks for the great answer, which I was really looking for... – Rajendra Badri Oct 18 '21 at 07:03
16

One correct way to get selected value would be

var selected_value = $('#fruit_name').val()

And then you should do

if(selected_value) { ... }
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
2

Another correct way to get selected value would be using this selector:

$("option[value="0"]:selected")

Best for you!

gmcheco
  • 79
  • 2
1

Check this answer its tested and working well

if( !$('#id').val() ) {
   // stuff here
}