Introduction:

In this article we will explain how to add text at the beginning of the selected elements in jquery or What is prepend() method in jquery or why use prepend() method in jquery with example. 

Description:

The jQuery prepend() method is used to add the specified content at the beginning of the selected elements. It is just the opposite of the jQuery append() method.If you want to add the content at the end of the selected elements, you should use the append method.

Syntax:

  • $(selector).prepend(“content”);
  • Example of jQuery prepend () method

Let's take an example to demonstrate jQuery prepend() Method.

 <!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btn1").click(function () {
                $("p").prepend("<b>Prepended text</b>. ");
            });
        });
    </script>
</head>
<body>
    <p>This is the tutorials point.</p>
    <p>This is the training point.</p>
    <button id="btn1">Prepend text</button>
</body>
</html> 

Output:

how to add content beginning of selected elements

In above example when we click on button box Prepended text insert at the beginning of the paragraph.

 

I hope this article will help to you.

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like