Jquery Add Element

Advertisements

Prev Tutorial Next Tutorial

Add Element

tutorial4us

Jquery have some predefined methods to add new elements or contents. It have following methods that are used to add new content;

  • append()
  • prepend()
  • after()
  • before()

append() method

This method are used to inserts content at the end of the selected elements

Example of append()

<!DOCTYPE html>
<html>
<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").append(" <b>Appended text</b>.");
    });
});
</script>
</head>
<body>

<p>This is my first program.</p>

<button>Append Text</button>

</body>
</html>

Output

This is my first program.

prepend() method

This method are used to inserts content at the beginning of the selected elements.

Example of prepend()

<!DOCTYPE html>
<html>
<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").prepend(" <b>Appended text</b>.");
    });
});
</script>
</head>
<body>

<p>This is my first program.</p>

<button>Append Text</button>

</body>
</html>

Output

This is my first program.

before() method

This method are used to inserts content before the selected elements.

Example of before()

<!DOCTYPE html>
<html>
<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").before(" <b>Text Add Before</b>.");
    });
});
</script>
</head>
<body>

<p>This is my first program.</p>

<button>Insert Text</button>

</body>
</html>

Output

This is my first program.

after() method

This method are used to inserts content after the selected elements.

Example of after()

<!DOCTYPE html>
<html>
<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").after(" <b>Text Add After</b>.");
    });
});
</script>
</head>
<body>


<p>This is my first program.</p>

<button>Insert Text</button>

</body>
</html>

Output

This is my first program.


Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements