You must Sign In to post a response.
  • Category: JQuery

    Get Id of div by class using jquery

    Hello friends,
    I have a div which generates runtime and I do not have access on code.But the html code rendered (checked in firebug) is as follows.


    <div tabindex="0" class="printButton printButton-up" role="button" aria-pressed="false" style="" aria-hidden="false"><input type="text" tabindex="-1" role="presentation" style="opacity: 0; height: 1px; width: 1px; z-index: -1; overflow: hidden; position: absolute;"><div class="html-face">Print</div></div>


    if I can write style="display:none" on above code then it will solve my purpose.
    I do not have ID of div in above code.can anyone help how can I achieve it


    Thanks
    Navneet
  • #759145
    Hi Navneet.

    <div tabindex="0" class="printButton printButton-
    up" role="button" aria-pressed="false" style=""
    aria-hidden="false"><input type="text"
    tabindex="-1" role="presentation" style="opacity:
    0; height: 1px; width: 1px; z-index: -1; overflow:
    hidden; position: absolute;"><div class="html-
    face">Print</div></div>

    var idbyclass = $(this).closest(".html-face").attr("id");

    var idbyclass = $(this).closest(".printButton printButton-up").attr("id");

    Mark as answer if you are satisfied.

    Regards

    Sridhar.
    DNS Member.
    "Hope for the best.. Prepare for the worst.."

    Sridhar Thota.
    Editor: DNS Forum.

  • #759148
    Hello Navneet,

    Refer my below code:

    HTML Markup:

    <div id="my_box_one" class="head-div">
    <div>
    <div class="some_box">a</div>
    <div class="some_box">b</div>
    </div>
    </div>

    JQuery:

    var abc = $(this).closest(".head-div").attr("id");

    Refer the below link to use closest:
    http://api.jquery.com/closest/

    Hope this will help you.
    Mark the answer if it helped you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #759153
    so you need to getElement by Class name and add attribute as 'display:none' right ?
    In Jquery with the help of 'Class Selector' you can Selects all elements with the given class, For class selectors, jQuery uses JavaScript's native getElementsByClassName() function if the browser supports it.
    see below snippet

    <body>
    <div class="notMe">div class="notMe"</div>
    <div class="myClass">div class="myClass"</div>
    <span class="myClass">span class="myClass"</span>
    <script>
    $( ".myClass" ).css( "border", "3px solid red" );
    </script>
    </body>

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments