jQuery before() insert created HTML Element

Description

jQuery before() 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>/*from  w  w  w.  j  av a2s. c o m*/
<script>
function beforeText() {
  var txt1 = "<b>I </b>";          
  var txt2 = $("<i></i>").text("love ");   
  var txt3 = document.createElement("b");  
  txt3.innerHTML = "jQuery!";
  $("img").before(txt1, txt2, txt3);  
}
</script>
</head>
<body>

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

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

<button onclick="beforeText()">Insert before</button>

</body>
</html>



PreviousNext

Related