locked
How to know the parent tag type RRS feed

  • Question

  • User264732274 posted

    suppose see my below html

    <body>
    
    <div id="dv1">
    
    <div id="dv2">
    
    </div>
    
    </div>
    
    </body>

    div2 parent tag is dv1 and dv1 parent tag is body. so tell me how do i know this by jquery......is it possible ?

    Wednesday, May 11, 2016 2:19 PM

Answers

  • User-491950272 posted

    You can achieve this either by:

        $('#dv2').parent().get(0).tagName
    

    or by known as:

    var iselement = $("#dv2").is("input")



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 11, 2016 2:30 PM
  • User475983607 posted

    As stated above the tagName property returns the tag name.

    http://www.w3schools.com/jsref/prop_element_tagname.asp

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
         <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
         <script type="text/javascript">
             $(function () {
                 var elem = $('#aDiv').parent();
                 console.log(console.log(elem.get(0).tagName););
             });
         </script>
    </head>
    <body>
        <div id="aDiv"></div>
    </body>
    </html>

    The above writes BODY to the console.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 11, 2016 3:08 PM

All replies

  • User-491950272 posted

    You can achieve this either by:

        $('#dv2').parent().get(0).tagName
    

    or by known as:

    var iselement = $("#dv2").is("input")



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 11, 2016 2:30 PM
  • User-286291038 posted

    You can also use the closest() function. Jquery documentation  which can help update your function accordingly given below,

    https://api.jquery.com/closest/

    https://api.jquery.com/parent/

    Wednesday, May 11, 2016 2:39 PM
  • User264732274 posted

    i like to know parent tag type. parent tag could be body, div, span or anything.

    Wednesday, May 11, 2016 2:41 PM
  • User475983607 posted

    As stated above the tagName property returns the tag name.

    http://www.w3schools.com/jsref/prop_element_tagname.asp

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
         <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
         <script type="text/javascript">
             $(function () {
                 var elem = $('#aDiv').parent();
                 console.log(console.log(elem.get(0).tagName););
             });
         </script>
    </head>
    <body>
        <div id="aDiv"></div>
    </body>
    </html>

    The above writes BODY to the console.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 11, 2016 3:08 PM