3

I have two input fields fields with the same class, one display:none. I want to query the value of the input field that is NOT display:none.

EDIT (with example)

<div id="Parent1" style="display:none">
  <p>
    <input type="text" class="title" value="">
  </p>
</div>

<div id="Parent2">
  <p>
    <input type="text" class="title" value="this is the one I want">
  </p>
</div>

js

$('.title').val();  

Returns blank since the first title class is empty. I want to ignore the first title who's parent is display:none.

Mark Kenny
  • 1,598
  • 2
  • 17
  • 30

3 Answers3

4

Use :visible selector to select element which are not hidden.

$('.class:visible')

Note:(from docs)

Elements are considered visible if they consume space in the document. Visible elements have a width or height that is greater than zero.

Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout.

Community
  • 1
  • 1
Tushar
  • 85,780
  • 21
  • 159
  • 179
0

there is a :visible selector in jquery (and a :not() selector, .i.e :not(:visible))

birdspider
  • 3,034
  • 1
  • 16
  • 25
0

Since you have two elements, you must iterate trough them and find the hidden one. I see this process convincing for me:

$('.class_el').each(function(el){
      if(el.hidden)
        //do something
});

You can also use the :visible selector, like !$(el).is(':visible') instead of reading the property of the element.

It's your choice! Cheers!