Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check whether a select box is empty using JQuery/Javascript
    primarykey
    data
    text
    <p>Using JQuery Mobile 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 calls a function via an 'on click' event. The job of this function is to know whether this particular check box has been populated or not; if it hasn't then it will simply not do anything, but otherwise it will run smoothly. My problem is determining whether or not this select box is empty or not.</p> <p>Here is a very simplified example of what I am dealing with:</p> <pre><code>&lt;li&gt; &lt;label for="fruit_name"&gt;Fruit&lt;/label&gt; &lt;select name="some_fruit" id="fruit_name" onclick="populate_box('fruit', this);"&gt; &lt;/select&gt; &lt;/li&gt; </code></pre> <p>My function, which is called from a separate button, looks like this:</p> <pre><code>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 } else { alert("null, value: " + selected_value); //do something } } </code></pre> <p>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. </p> <p>For instance using <code>document.getElementById(...)...</code> will not necessarily return false and it depends on how you use it. Also using <code>$("#someID")...</code> in jQuery may or may not produce the desired results. I have already tried many different times as you can see in the <em>commented lines</em>, all of which can be evaluated in the <code>if(...)</code> statement.</p> <p>Do you have any suggestions or ideas on how to go about achieving this? Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload