jQuery after() insert created HTML element

Description

jQuery after() insert created HTML element

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/* w ww  .  ja v a  2s  . co  m*/
<script>
function afterText() {
  var txt1 = "<b>I </b>";           
  var txt2 = $("<i></i>").text("love ");  
  var txt3 = document.createElement("b"); 
  txt3.innerHTML = "jQuery!";
  $("img").after(txt1, txt2, txt3);    // Insert new elements after img
}
</script>
</head>
<body>

<img src="image4.png" alt="jQuery" width="100" height="140">

<p>Click the button to insert text after the image.</p>

<button onclick="afterText()">Insert after</button>

</body>
</html>



PreviousNext

Related