w3resource

jQuery: Make first word bold of all elements

jQuery Practical exercise Part - I : Exercise-10

Make first word bold of all elements.

Sample HTML Code :

<body>
<p>PHP Exercises</p>
<p>Python Exercises</p>
<p>JavaScript Exercises</p>
<p>jQuery Exercises</p>
</body>

Expected Output :

PHP Exercises

Python Exercises

JavaScript Exercises

jQuery Exercises

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>Make first word bold of all elements</title>
</head>
<body>
  <p>PHP Exercises</p>
  <p>Python Exercises</p>
  <p>JavaScript Exercises</p>
  <p>jQuery Exercises</p>
</body>
</html>

JavaScript Code :

$('p').each(function(){
	var pdata = $(this);
	pdata.html( pdata.text().replace(/(^\w+)/,'$1') );
});

Go to:


PREV : Limit character input in the textarea including count.
NEXT : Create a division using jQuery with style tag.

See the Pen jquery-practical-exercise-10 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.