- Filtering Elements That Do Not Contain Any Elements or Text
Finding empty elements in the DOM can be useful. If you have an empty element on your page, you can use the :empty filter to find it. In the following HTML example, I want to hide the error div if it is empty. I just need to select the .error class and add the :empty filter. If the error message has content, it remains showing; otherwise, the display:none CSS is applied to it.
<!doctype html>
<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script>
$(document).ready(function() {
$('.error:empty)').css('display','none');
});
</script>
- Filtering Elements That Contain Text
In some cases, you may want to filter selectors based on the text that the element contains. You can do so using the :contains filter. Th e text that you pass to the :contains filter can be enclosed in quotes or just plain text. In the following HTML example, I want to hide the error div if it's empty. I just need to select the .error class and add the :empty filter. If the error message has content, it displays — otherwise, the display:none CSS is applied to it.
<!doctype html>
<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></
script>
<script>
$(document).ready(function() {
$("tr td:contains('Wet tissue')").css('border','1px dashed #111');
});
</script>
Read Again :
How to Filtering DOM Elements Using jQuery Selector Filters - Part 1
0 comments:
Post a Comment