With JQuery, We can easily create different HTML elements and also insert them into components.
<!DOCTYPE html>
<html>
<head>
<title>Errorsea - jquery create element</title>
</head>
<body>
</body>
</html>
Above structure is the basic HTML structure that contains the head and body. To use jquery in HTML code, we require jquery google CDN in head or body section.
Step 1:
So, first, insert the following CDN into head tag.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
Note: We require above CDN in our code. JQuery CDN requires an active internet connection.
<!DOCTYPE html>
<html>
<head>
<title>Errorsea - jquery create element</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
</body>
</html>
Step 2:
Suppose we want to insert a paragraph inside body tag using jquery.
Here, we create elements using jquery as follows:
<script>
$(document).ready(function(){
var p1 = $("<p>This is a paragraph</p>");
$("body").append(p1);
});
</script>
Explanation:
<!DOCTYPE html>
<html>
<head>
<title>Errorsea - jquery create element</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
var p1 = $("<p>This is a paragraph</p>");
$("body").append(p1);
});
</script>
</body>
</html>
We can also insert the second paragraph inside the newly created paragraph using jquery.
<script>
$(document).ready(function(){
var p1 = $("<p>This is a paragraph</p>");
var p2 = $("<b>This is inside paragraph</b>");
p1.append(p2);
$("body").append(p1);
});
</script>
Explanation:
<!DOCTYPE html>
<html>
<head>
<title>Errorsea - jquery create element</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
var p1 = $("<p>This is a paragraph.</p>");
var p2 = $("<b>This is inside paragraph</b>");
p1.append(p2);
$("body").append(p1);
});
</script>
</body>
</html>
Read Also: JQuery Remove Element
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…