search box

The Webmistress's picture

She has: 5,586 posts

Joined: Feb 2001

Does anyone know how to control a text box within a form (search facility) so that the box has an initial value but when the user clicks in the box the initial value is deleted?

At the moment as it is the initial value has to be highlighted by the user and then they type in their search value.

Ideas???

Julia - if life was meant to be easy Michael Angelo would have painted the floor....

Busy's picture

He has: 6,151 posts

Joined: May 2001

onBlur or onFocus ??

been playing with Javascript and PHP (combined) all day so mind is a bit scrambled at the moment

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Yeah, I have the script you want (I think) on my page

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

you can use onfocus="select()" but it's not a hundred percent as if the focus happens with a click rather than a tab, the click can interfere. That selects all the words in the field so the next character typed will clear the field except for that character.

You can also set onclick the same way...

So...

This should be selected when the field is active.

This should go away when the field is active.

Caveats would be that this plain code will also remove anything the user has typed.

I prefer the select(); option and use it instead of other options, but the function on Renegade's page (you need internal targets, Renegade) handles this problem well, but testing to see if the value is the same as the default. If so, it clears it. If not, it leaves it the heck alone... Smiling

druagord's picture

He has: 335 posts

Joined: May 2003

Suzanne the code you gave is working but select() is useless you could just write

This should go away when the field is active.

since you delete the value you don't need to select it Smiling

IF , ELSE , WHILE isn't that what life is all about

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Ack, very true! I meant to use one or the other -- i.e. you can use select(); or this.value=''; as your options.

*rolling eyes at self*

I shouldn't post uncaffeinated! I've edited my post to reflect the two options separately and clear up ambiguous wording. Thanks, Druagord.

druagord's picture

He has: 335 posts

Joined: May 2003

My post wasn't meant to critisize you, just to optimize the code. Yes caffeine is a programmer very good friend Smiling

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

lol, criticize me all you want, it's good for me!

The Webmistress's picture

She has: 5,586 posts

Joined: Feb 2001

So what's wrong with Renegade's code? I understand that one more than the others!

druagord's picture

He has: 335 posts

Joined: May 2003

nothing is wrong with renegades code. Suzanne's is just more simple

The Webmistress's picture

She has: 5,586 posts

Joined: Feb 2001

So with Suzanne's code I just simply add that to the code I already have:

<input name="query" type="text" class="search" value="   Search This Site" size="33" />'

Julia - if life was meant to be easy Michael Angelo would have painted the floor....

druagord's picture

He has: 335 posts

Joined: May 2003

euh i'm not sure what you mean but it should look like this in the end

<input name="query" type="text" class="search" value="   Search This Site" size="33" onfocus="this.value=''"  />
'

you can add the onclick to but i'm not sure you really need it

IF , ELSE , WHILE isn't that what life is all about

The Webmistress's picture

She has: 5,586 posts

Joined: Feb 2001

Great I'll try that! Thanks for your help guys Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.