Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last visible element using jQuery

<table>
    <tr class="here" id="t1" number="1" ><td>1</td></tr>
    <tr class="here" id="t2" number="2" ><td>2</td></tr>
    <tr class="here" id="t3" number="3" style="display:none"><td>3</td></tr>
    <tr class="here" id="t4" number="4" style="display:none"><td>4</td></tr>
</table>

<span id="check">check</span>

$('#check').click(function(){        
    check = ???;
   alert(check);
})

DEMO: http://jsfiddle.net/vUukc/1/

How can I get the attribute number from last visible tr in this example? This is an example - all <tr> could be visible.

like image 782
Roy Grendester Avatar asked Jun 22 '12 10:06

Roy Grendester


People also ask

How do I get only visible elements in jQuery?

To achieve the best performance when using :visible to select elements, first select the elements using a pure CSS selector, then use . filter(":visible") . Using this selector heavily can have performance implications, as it may force the browser to re-render the page before it can determine visibility.

Which jQuery filter can be used to check if element is visible?

Answer: Use the jQuery :visible Selector You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.

Is visible condition in jQuery?

Syntax: $(element).is(":visible"); Example 1: This example uses :visible selector to check an element is visible or not using jQuery.

Is visible selector jQuery?

The :visible selector in jQuery is used to select every element which is currently visible. It works upon the visible elements. The elements that are consuming space in the document are considered visible elements. The height and width of visible elements are larger than 0.


2 Answers

There are different jQuery Selectors for those purposes.

For example:

$('table tr:visible:last').attr('number');

or

$('table tr:visible').last().attr('number');

and so on.

Full code

$('#check').click(function() {
    check = $('table tr:visible:last').attr('number');
    alert(check);
});

DEMO

like image 129
thecodeparadox Avatar answered Oct 14 '22 04:10

thecodeparadox


$("table tr:visible:last").attr("number");

See the jQuery Documentation on Selectors for more info on the :visible and :last selectors.

like image 5
Cranio Avatar answered Oct 14 '22 06:10

Cranio