Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an empty JQuery object

Tags:

jquery

People also ask

Is Empty jQuery object?

jQuery isEmptyObject() methodThe isEmptyObject() method is used to determine whether the passed argument is an empty object or not. It returns a Boolean value. If it finds the passed value is an empty object, it returns true. Otherwise, it returns false.

How check data is empty or not in jQuery?

if(data && data != "") alert(data);

How check JSON object is empty or not in jQuery?

if(d. DESCRIPTION == 'null'){ console. log("Its empty");

Is Empty object Javascript?

Use a for...in loop to iterate over the properties of the object. If there is even a single iteration, the object is not empty. If there aren't any iterations, the object is empty.


This creates an empty jQuery-object:

$([])

Update: In newer versions of jQuery (1.4+), you can use:

$()

$();

Returning an Empty Set

As of jQuery 1.4, calling the jQuery() method with no arguments returns an empty jQuery set (with a .length property of 0). In previous versions of jQuery, this would return a set containing the document node.

Source: api.jquery.com


My advice is don't do it that way. There are a lot easier ways of doing this. Consider:

<select id="select" name="select">
  <option value="msg_1">Message 1</option>
  <option value="msg_2">Message 1</option>
  <option value="msg_3">Message 1</option>
</select>

<div class="msg_1 msg_3">
  ...
</div>

<div class="msg_1">
  ...
</div>

<div class="msg_2">
  ...
</div>

$(function() {
  $("#select").change(function() {
    var val = $(this).val();
    $("div." + val").show();
    $("div:not(." + val + ")").hide();
  });
});

Much easier. Basically give classes to indicate what to show and hide and then there is no tracking required. An alternative is:

$(function() {
  $("#select").change(function() {
    var val = $(this).val();
    $("div").each(function() {
      if ($(this).hasClass(val)) {
        $(this).show();
      } else {
        $(this).hide();
      }
    });
  });
});