Add plain HTML, jQuery object and Javascript DOM - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:prepend

Description

Add plain HTML, jQuery object and Javascript DOM

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function prependText() {//from   ww w.ja  v a  2  s  .c  o m
    var txt1 = "<p>plain html</p>";              // Create text with HTML
    var txt2 = $("<p></p>").text("jQuery object.");  // Create text with jQuery
    var txt3 = document.createElement("p");
    txt3.innerHTML = "Javascript DOM.";               // Create text with DOM
    $("p").prepend(txt1, txt2, txt3);       // Prepend new elements
}
</script>
</head>
<body>

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

<button onclick="prependText()">Prepend text</button>

</body>
</html>

Related Tutorials