0

I'm writing a CSS selector, which I have working so far:

input[placeholder*='mail']

but I'd like to ensure it's not finding invisible (ie: not visible) elements.

I've been trying :visible in various places in the pattern (because I couldn't find a good reference on the CSS selector lexer, but not luck with these:

input[placeholder:visible*='mail']
input:visible[placeholder*='mail']
input[placeholder*='mail']:visible

How do I do this? And anyone have a good reference on learning more complex selector formats?

lollercoaster
  • 15,969
  • 35
  • 115
  • 173

3 Answers3

1

There is no CSS selector for :visible. You sould then work with classes and target the elements which have the class .visible. (Or NOT have the class .visible.

The :visible selector is only available in jQuery for example, which uses the pseudo selector for finding elements visible in current scroll view.

Jelle Posthuma
  • 153
  • 1
  • 8
  • Hmm, I really just want to filter out invisible DOM elements and only select those that are visible. Is there a way to do that? – lollercoaster Jul 27 '17 at 22:58
1

You can't do it with only CSS.

But you can do it with jquery and most probably this answer can help you.

pacanga
  • 416
  • 6
  • 17
1

:visible is a jQuery selector. Not a CSS one.

And you can't use it on an element's attribute like placeholder.

To check if there is an inputted value (which makes the placeholder "not visible"), you need to use some client-side code.

The jQuery would look like this:

$("input[placeholder*='mail']").each(function(){
  if( $(this).val() != "" ){
    // Do something.
    // ...
  }
});



To "filter out invisible elements" and keep only the visible ones:

var visible = $("input[placeholder*='mail']:visible").length;
console.log(visible+" elements are visible.");
.hidden{
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="one" placeholder="My mail">
<input type="text" id="two" placeholder="Your mail">
<input type="text" id="three" class="hidden" placeholder="Junk mail">
<input type="hidden" id="four" placeholder="Cool mail">
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64