w3resource

jQuery: Find the title attribute of the first emphasized in the page

jQuery Fundamental - I : Exercise-28

Find the title attribute of the first <em> in the page.

<body>
<p>jQuery Exercises, Practice,  <em title="Real  life jQuery or  all.">Solution</em>.</p>
<p  id="id1"></p>
<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>Find the title attribute of the first em in the page.</title>
<style type="text/css">
button {
 display: block;
 margin: 20px 0 0 0;
  }
  </style>
  </head>
  <body>
  <p>jQuery Exercises, Practice, <em title="Real life jQuery or all.">Solution</em>.</p>
  <p id="id1"></p>
  <button id="button1">Click to see the effect</button>
  </body>
</html>

JavaScript Code :

$('#button1').click(function(){ 
var title = $( "em" ).attr( "title" );
$("#id1").text( title );});

Used Methods :

  • .attr() : Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.

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


Contribute your code and comments through Disqus.

Previous: Using jQuery display the checked attribute and property of a checkbox as it changes.
Next: Using jQuery find all links with an hreflang attribute "de".

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/jquery-exercises/1/jquery-fundamental-exercise-28.php