w3resource

jQuery: Stop people from writing in a text box

jQuery Events : Exercise-10 with Solution

Stop people from writing in first text input box (user ID).

Sample Data :

<body>
<p>User ID : <input type="text" id='field1'></p>
<p>Password : <input type="password" id='field2'>
</body>

Solution:

HTML Code:


<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>Stop people from writing in first text input box</title>
</head>
<body>
<p>User ID : <input type="text" id='field1'></p>
<p>Password : <input type="password" id='field2'> </p>
</body>
</html>

JavaScript Code :


$( "input[type=text]" ).focus(function() {
  $( this ).blur();
});

Live Demo:

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


Contribute your code and comments through Disqus.

Previous: Find the position of the mouse pointer relative to the left and top edges of the document.
Next: Cancel the default action (click) of the first URL.

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.