Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select all the empty tag using jQuery.? [duplicate]

Tags:

html

jquery

How can I select all the empty tag using jQuery.

I want to select

<p></p>
<p style="display: block"></p>
<p> </p>
<p>     </p>
<p>&nbsp;</p>

and not

<p>0</p>
<p>Test</p>

I tried with :empty but it is not working with my options. Any help on this is greatly appreciated.

like image 418
laradev Avatar asked Aug 26 '13 05:08

laradev


3 Answers

You can do this using jQuery.filter().

var empty_p = $('p').filter(function(){
   return !$.trim($(this).text());
}).get();
like image 114
Dipesh Parmar Avatar answered Nov 12 '22 05:11

Dipesh Parmar


Use jQuery filter:

$('p').filter(function () {
   return !$(this).text().trim();
});
like image 45
Artyom Neustroev Avatar answered Nov 12 '22 04:11

Artyom Neustroev


$(document).ready(function(){
    $("p").each(function(index){
        var html = $(this).html();
        html = filter(html, " ");
        html = filter(html, "&nbsp;");

        if ($(this).html() == ""){
            //YOUR CODE
        }
    });
});

function filter(string, char){
    while(string.indexOf(char) > 0){
        string.replace(char, "");
    }
}

JS FIDDLE

like image 1
Seano666 Avatar answered Nov 12 '22 03:11

Seano666