w3resource
jquery exercises

jQuery: Insert a jQuery object before all paragraphs

jQuery Fundamental - I : Exercise-38

Inserts a jQuery object before all paragraphs.

Note: Here object is similar to an Array of DOM Elements.

<body>
<p>PHP Tutorial</p><b>w3resource</b>
<button id="button1">Click to see the effect</button> 
</body>

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Inserts a jQuery object before all paragraphs.</title>
<style type="text/css">
button {
 display: block;
  margin: 20px 0 0 0;
   }  
</head>
<body>
<p>PHP Tutorial</p><b>w3resource</b>
<button id="button1">Click to see the effect</button>
</body>
</html>

JavaScript Code :

$('#button1').click(function(){ 
$( "p" ).before( $( "b" ) );
});

Used Methods :

  • .before() : Insert content, specified by the parameter, before each element in the set of matched elements.

See the Pen jquery-fundamental-exercise-38 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Inserts a DOM element before all paragraphs using jQuery.
Next: Demonstrate how to handle click and double-click on a paragraph.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming