Question

This is my jQuery code:

$('input[name="Search"]').blur(function(e) {
    $(this).text('');
});

And my HTML snippet:

<input name="Search" type="text" id="Search" class="MainMenu" />

But it doesn't work, does anybody know why?

Was it helpful?

Solution 2

You are using text() on input type text which is not defined for input type=text. You need to use val()

Live Demo

$('input[name="Search"]').blur(function(e) { 
 $(this).val('');
});​

If you have id, you can use id as it will be more efficient then getting element with name selector.

Live Demo

$('#Search').blur(function(e) { 
    $(this).val('');
});​ 

OTHER TIPS

try .val() insteadof .text()

$('input[name="Search"]').blur(function(e) { 
 $(this).val('');
});​

there is no method like .text() for input type text use .val()

these change will help you

$('input[name="Search"]').blur(function(e) {
    $(this).val('');
});

or

$('#Search').blur(function(e) { 
    $(this).val('');
});​ 

Documentation here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top