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> </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.
You can do this using jQuery.filter()
.
var empty_p = $('p').filter(function(){
return !$.trim($(this).text());
}).get();
Use jQuery filter:
$('p').filter(function () {
return !$(this).text().trim();
});
$(document).ready(function(){
$("p").each(function(index){
var html = $(this).html();
html = filter(html, " ");
html = filter(html, " ");
if ($(this).html() == ""){
//YOUR CODE
}
});
});
function filter(string, char){
while(string.indexOf(char) > 0){
string.replace(char, "");
}
}
JS FIDDLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With