Question User Question User - 3 months ago 9
jQuery Question

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

Answer

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>