0

https://jsfiddle.net/huat81z4/

CODE HTML:

<input class="search" value="Search" type="text" size="18" name="s" id="s">

CODE JS:

$(document).ready(function(){
     $('input .search).click(function(){
             $(".this").attr("value", "");
      });
});

I want to disappear the word "Search" when the user clicks on the form. What is wrong with my code?

Is there any function that makes it better?

2
  • 1
    Your string isn’t closed. Commented May 21, 2015 at 12:23
  • $(".this") ? $('input .search') For this case use placeholder attribute Commented May 21, 2015 at 12:23

10 Answers 10

3

use placeholder attribute

<input class="search" placeholder="Search" type="text" size="18" name="s" id="s">
Sign up to request clarification or add additional context in comments.

1 Comment

excelent tip, JS is not necessary
0

Just use placeholder attribute instead of value.

<input class="search" placeholder="Search" type="text" size="18" name="s" id="s">

Or if you want to do it in a script do it like that:

$(document).ready(function(){
     $('.search').click(function(){
             $(this).attr("value", "");
      });
});

Comments

0

You could try

<input class="search" placeholder="Search" type="text" size="18" name="s" id="s">

This will put a placeholder text inside your text input, and when someone tries to enter anything, it will be gone.

But if you want to stick to js, then:

$(document).ready(function(){
     $('input.search').click(function(){
             $(this).attr("value", " ");
      });
});

You had a few typos in your code.

Comments

0

Use html5 placeholder

<input class="search" placeholder="Search" type="text" size="18" name="s" id="s">

And make correct your code with this

<script>
$(document).ready(function(){
     $('input .search').click(function(){
             $(this).attr("value", "");
      });
});
</script>

You forget to ' in search and $(".this") to $(this)

Comments

0

You can simply use Placeholder and also there is a typo in your code:-

$(".this").attr("value", ""); :- Incorrect

$(this).attr("value", ""); :- correct

Comments

0

I think you better use the HTML attribute placeholder like all the other answers suggest.

Here I'll explain what's wrong with your JavaScript:

$(document).ready(function(){
     $('input .search).click(function(){ // the string isn't closed and you have to use 'input.search', because 'input .search' looks for a child element of an input
             $(".this").attr("value", ""); // it's only this, not ".this"
      });
});

This code works

$(document).ready(function(){
     $('input.search').focus(function(){
             $(this).attr("value", "");
      }); 
});

Demo: https://jsfiddle.net/rkxmby1f/

Comments

0

You have some typo mistake so it's not working:

 $('.search').click(function(){
        $(this).attr("value", "");
  });

First: $('input .search) you didn't close '.

Second: Use $(this) instead of $(".this")

Check Your updated Fiddle here.

Note: I suggest to use placeholder instead of it.

Hope it helps.

Comments

0

You want it to disappear immediate after your click,

Here it is using JavaScript

JS : DEMO

$(".search").click(function()
{
   $(".search").prop("value","");
});

OR

Placeholder attribute of HTML

HTML

<input class="search" placeholder="Search" type="text" size="18" name="s" id="s"> 

Note: It will only disappear after you start typing.

Comments

0
$(document).ready(function(){

    $('.search').on('focus', function() {
       $(this).val(""); 
    });

});

Comments

0

You can use this too, it is simple ...

<input class="text-input" type="text" value="Search" onfocus="if(this.value == 'Search') this.value = '';" onblur="if(this.value=='') this.value='Search';"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.