Showing posts with label jQuery Accordion. Show all posts
Showing posts with label jQuery Accordion. Show all posts

Monday, July 28, 2014

Difference between parent() vs parents()

Below code clarify some differences between the parent() and parents() methods.

parent(selector) : This returns the one immediate parent of the element. Selector is passed in only required cases, like if we want to check if parent exists for the element or in some other situations. Here is simple example which illustrates the parent method.



parents(selector) : This returns the multiple ancestors. Its not restricted to only one level of parent/ancestors of the element. This is the main difference between the parent and parents method.

For Examples 1
<html>…<body><div class=”one”><div class=”two”><p><span>Some text</span></p></div></div></body></html>

$(‘span’).parent() will select the <p> tag such that the element set in the jQuery object is [span].

$(‘span’).parents() will select all parent tags such that the element set in the jQuery object is [p, div.two, div.one, body, html].

So parent() will select the first parent element while parents() will select all elements straight up the DOM tree.

The .parents() and .parent() methods are similar, except that the latter only travels a single level up the DOM tree. Also, $( "html" ).parent() method returns a set containing document whereas $( "html" ).parents() returns an empty set.


For Examples 2


HTML Code

<!DOCTYPE html>
<html lang="en">
  <head>
     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  </head>
  <body>
     <div>
        <p>
            <a href="#" id="anchorTag">Click here</a>
         </p>
      </div>
   </body>
</html>


jQuery  For parent()
   $('#anchorTag').parent();      //This would return p, immediate parent of a#anchorTag.


jQuery  For parents()
   $('#anchorTag').parents();        //This would return all the ancestors (p, div, body, html)

   //On passing selector it will filter down the matched parent elements by selector

   $('#anchorTag').parents('div');   //This would give me only div




 closest(selector) : It works like parents(), except that it returns only one parent/ancestor. This is ideal for the situation i mentioned earlier. If i want to check for existence of element in the ancestry tree, then i would prefer to use the closest rather than parents as it only target the selector rather than filtering it from all the ancestor elements. Here is example

$('#anchorTag').closest();           //This would give me empty object as we have not mentioned the selector
$('#anchorTag').closest('div');      //This would give me the div.

Saturday, April 26, 2014

Drop down content with onclick using javascript

This is very simple drop down code using html, javascript,css. This code opening/closing container  every drop down onclick heading.


<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8'/>
        <title>Drop down content with onclick using javascript</title>
        <style>
            * { margin:0; padding:0; font-family:sans-serif; }

            body { width:650px; }

            .drop_h, li, h2 { margin-bottom:15px; }

            .drop_h {
                text-decoration:none;
                font-size:16px;
                display:block;
            }

            .content {
                display:none;
            }

            #improved li {
                position:relative;
                overflow:hidden;
            }
        </style>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $('#original .drop_h').click(function(e){
                    e.preventDefault();
                    $(this).closest('li').find('.content').slideToggle();
                   
                   
                   
                   
                   
                   
                   
                   
                });

                $('#improved .drop_h').click(function(e){
                    e.preventDefault();
                    $(this).closest('li').find('.content').not(':animated').slideToggle();
                });
            });
        </script>
    </head>
    <body>
        <h2>Drop Down Simple Script</h2>
        <ul id='original'>
            <li>
                <a href='#' class='drop_h'>Heading 1</a>
                <div class='content'>
                    <p>PHP5 introduces 'interfaces' . An interface defines the methods a class must implement. All the methods defined in an interface must be public. An interface helps you design common APIs. It is not designed as a blueprint for classes, but just a way to standardize a common API. A big advantage of using interfaces is that a class can implement any number of interfaces. You can still only 'extend' on parent class, but you can 'implement' an unlimited number of interfaces.</p>
                </div>
            </li>
            <li>
                <a href='#' class='drop_h'>Heading 2</a>
                <div class='content'>
                    <p>PHP5 introduces a special function called '__autoload()' (the word 'autoload' prefixed by double underscores). This function allows you to avoid writing a long list of includes at the top of your script by defining them inside this function. So you can automatically load object files when PHP encounters a class that hasn't been defined </p>

                    <p>PHP5 introduces 'interfaces' . An interface defines the methods a class must implement. All the methods defined in an interface must be public. An interface helps you design common APIs. It is not designed as a blueprint for classes, but just a way to standardize a common API. A big advantage of using interfaces is that a class can implement any number of interfaces. You can still only 'extend' on parent class, but you can 'implement' an unlimited number of interfaces.</p>
                </div>
            </li>
           
        </ul>
        <h2>Animated Drop Down </h2>
        <ul id='improved'>
            <li>
                <a href='#' class='drop_h'> Improved Heading 1</a>
                <div class='content'>
                    <p>PHP5 introduces 'interfaces' . An interface defines the methods a class must implement. All the methods defined in an interface must be public. An interface helps you design common APIs. It is not designed as a blueprint for classes, but just a way to standardize a common API. A big advantage of using interfaces is that a class can implement any number of interfaces. You can still only 'extend' on parent class, but you can 'implement' an unlimited number of interfaces.</p>
                </div>
            </li>
            <li>
                <a href='#' class='drop_h'>Improved Heading 2</a>
                <div class='content'>
                    <p>PHP5 introduces a special function called '__autoload()' (the word 'autoload' prefixed by double underscores). This function allows you to avoid writing a long list of includes at the top of your script by defining them inside this function. So you can automatically load object files when PHP encounters a class that hasn't been defined </p>

                    <p>PHP5 introduces 'interfaces' . An interface defines the methods a class must implement. All the methods defined in an interface must be public. An interface helps you design common APIs. It is not designed as a blueprint for classes, but just a way to standardize a common API. A big advantage of using interfaces is that a class can implement any number of interfaces. You can still only 'extend' on parent class, but you can 'implement' an unlimited number of interfaces.</p>
                </div>
            </li>
           
        </ul>
    </body>
</html>