jQuery prepend() method

The prepend() method inserts specified content at the beginning of the selected elements.To insert content at the end of the selected elements, use the append() method.

Syntax:


$(selector).prepend(content,function(index,html))


prepend() and prependTo() methods are doing the same task, add a text or HTML content before the content of the matched elements. 
So we can say that The jQuery prepend() method is used to insert the specified content at the beginning (as a first child) of the selected elements. It is just the opposite of the jQuery append() method.


Example :

<!DOCTYPE html>

<html>

<head>

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

  <script>

$(document).ready(function(){

    $("#btn11").click(function(){

        $("p").prepend("<b>Prepended text ..</b>. ");

    });

    $("#btn22").click(function(){

        $("ol").prepend("<li>Prepended item...</li>");

    });

});

  </script>

</head>

<body>


  <p>This is a paragraph. hi friends how are you ?</p>

  <p>This is another paragraph....</p>


  <ol>

    <li>List item 11</li>

    <li>List item 22</li>

    <li>List item 33</li>

  </ol>


  <button id="btn11">Prepend text</button>

  <button id="btn22">Prepend list item</button>


</body>

</html>

jQuery after() method

The after() method inserts specified content after the selected elements.To insert content before selected elements, use the before() method.The  .after()  and .insertAfter() methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With .after(), the selector expression preceding the method is the container after which the content is inserted. With .insertAfter(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted after the target container.

Syntax:


$(selector).after(content,function(index))




Example :

<!DOCTYPE html>

<html>

<head>

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

  <script>

$(document).ready(function(){

    $("button").click(function(){

        $("p").after("<p>Hello world! How Are You ?</p>");

    });

});

  </script>

</head>

<body>


  <button>Insert content after each p element</button>


  <p>This is a paragraph.  hi friends How are you ?.</p>

  <p>This is another paragraph........</p>


</body>

</html>

jQuery fadeToggle() method

The fadeToggle() method toggles between the fadeIn() and fadeOut() methods.If the elements are faded out, fadeToggle() will fade them in.If the elements are faded in, fadeToggle() will fade them out.Hidden elements will not be displayed at all (no longer affects the layout of the page).

So jQuery fadeToggle() method is used to toggle between the fadeIn() and fadeOut() methods. If the elements are faded in, it will make them faded out and if they are faded out it will make them faded in.

Syntax:


$(selector).fadeToggle(speed,easing,callback)




Example :



<!DOCTYPE html>

<html>

<head>

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

  <script>

$(document).ready(function(){

    $("button").click(function(){

        $("#div11").fadeToggle();

        $("#div22").fadeToggle("slow");

        $("#div33").fadeToggle(3000);

    });

});

  </script>

</head>

<body>


  <p>Demonstrate fadeToggle() with different speed parameters.</p>


  <button>Click me to fade in/out boxes</button><br><br>


  <div id="div11" style="width:100px;height:100px;background-color:blue;"></div>

  <br>

  <div id="div22" style="width:100px;height:100px;background-color:purple;"></div>

  <br>

  <div id="div33" style="width:100px;height:100px;background-color:yellow;"></div>


</body>

</html>