Home  >  Article  >  Web Front-end  >  How to verify if an element is empty in jquery

How to verify if an element is empty in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-18 14:55:082028browse

Jquery method to verify whether an element is empty: 1. Use [val()] to judge input, and the code is [var value = $('#test').val()]; 2. Use [html ()] Judgment, the code is [var value = $('#test').html()].

How to verify if an element is empty in jquery

The operating environment of this tutorial: windows10, jquery2.2.4, this article is applicable to all brands of computers.

Jquery method to verify whether an element is empty:

Input uses val();

var value = $('#test').val();

Whether it is empty Judgment method:

  • if(value.length == 0){} Operation performed if value is empty

  • if(value!= ''){} If the value is not empty, the operation performed

html element is judged by html();

var value = $('#test').html();

is empty. Method:

if(value.length == 0){

You can also judge whether there are child nodes? For html acquisition methods, you can use

$('#list').children().length === 0

Method 1


    Method 2,

    String.prototype.isEmpty = function () {
     var s1 = this.replace(/[\r\n]/g, '').replace(/[ ]/g, ''),
       s2 = (s1 == '') ? true : false;
     return s2;
    };
    $list.html().isEmpty();

    if( $("#list").html() === "" ){} Using three equal signs is more in line with

    Related free learning recommendations: JavaScript (video)

    The above is the detailed content of How to verify if an element is empty in jquery. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn