Question User
Question User

Reputation: 2293

how can i get the current displayed div id in jquery

How can i get the the blocked div id in jquery

HTML

 <div id="USDC1" style="" class="tablegraphview">USDC2</div>
 <div id="USDC2" style="display:none" class="tablegraphview">USDC2</div>
 <div id="USDC3" style="display:none" class="tablegraphview">USDC3</div>

Javascript

function tablegraphview(){
var getid = $('.tablegraphview').attr('id');
alert(getid);
}

for ex

<div id="USDC2" style="display:block" class="tablegraphview">USDC2</div>

I need to current div id how can i do that in jquery

Upvotes: 0

Views: 4110

Answers (2)

Mani
Mani

Reputation: 2655

Try with $(this).is(':visible')

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="USDC1" style="" class="tablegraphview">USDC2</div>
<div id="USDC2" style="display:none" class="tablegraphview">USDC2</div>
<div id="USDC3" style="display:none" class="tablegraphview">USDC3</div>

<script>
$(function() {

    $(".tablegraphview").each( function(){
        if($(this).is(':visible')){
            alert($(this).attr("id"));
        }
    });

});
</script>

Or

As per @pieter command

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div id="USDC1" style="" class="tablegraphview">USDC2</div>
    <div id="USDC2" style="display:none" class="tablegraphview">USDC2</div>
    <div id="USDC3" style="display:none" class="tablegraphview">USDC3</div>
    
    <script>
    $(function() {
    
    	alert($(".tablegraphview:visible").attr('id'));
    
    });
    </script>

Upvotes: 3

Amar Singh
Amar Singh

Reputation: 5622

Use this selector

 $('div').not('[style*="display:none"]')

Working Fiddle

Upvotes: 0

Related Questions