0

I know others have asked about this. And I have tried the solutions that others have recommended, but it doesn't seem to work.

Here is my code:

<!DOCTYPE HTML>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script type='text/javascript'>
$('input:text').focus(
    function(){
        $(this).val('');
});
</script>
</head>
<body>
<form>
<input type="text" value="Text">
</form>
</body>
</html>

2 Answers2

3

Your problem is that when the jQuery code runs the html is not there yet. So you need to run the jQuery only when the page loaded, or post that script just before the <body> closing tag.

Option 1 (add a DOM ready call to yor function):

$(document).ready(function() {
    $('input:text').focus(function() {
        $(this).val("");
    });
});

Option 2 (to add the <script> before the closing of body tag)

<!DOCTYPE HTML>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<form>
<input type="text" value="Text">
</form>
<script type='text/javascript'>
$('input:text').focus(
    function(){
        $(this).val('');
});
</script>
</body>
</html>
Sergio
  • 27,160
  • 10
  • 79
  • 126
2

You want to do something more along the lines of

$(function() {
    $('input:text').focus(function() {
        $(this).val("");
    });
});

without the $(function() {... it is possible that you are trying to fire jQuery before it has loaded.

GaryDevenay
  • 2,320
  • 2
  • 18
  • 39
  • Still doesn't work. Get this error in console: Uncaught TypeError: Object # has no method 'sendMessage' – Spotify Bruger Aug 26 '13 at 14:37
  • That isn't an error related to this issue. We are not calling the method sendMessage here. This must be a javascript error from elsewhere in your application. – GaryDevenay Aug 26 '13 at 14:38