How to add pretext and recall in search box {HELP}

Hi,

I am trying to add pretext to the search box, but have it clear when the user clicks the search box to enter their own search term, and have the pretext return if the user exits the search box. Any help will be much appreciated.

Thanks

You mean something link this http://jsfiddle.net/fastsol/AAS8C/
This will remove the default value text and reapply it on focusout if it’s empty, but won’t remove it if the text is different than the default text.

Hi

Yes Exactly. Can I just put the JavaScript within the php? or does it go in the head section?

Thanks Again for helping out.

It’s generally better to keep the javascript in the head if you can but you can certainly do it inline if needed. Make sure to wrap the code shown in the fiddle in a document.ready scenario cause that is not shown in the fiddle.
So like this if you don’t understand

$(document).ready(function(){
  // Code I provided in here.
});

So it should look like this:

$(document).ready(function(){
var orig_text = $(’#SearchTearm’).val();
$(’#SearchTearm’).focus(function(){
var current_text = $(this).val();
if(current_text != orig_text){}
else{ $(this).val(’’); }
}).focusout(function(){
var current_text = $(this).val();
if(current_text == ‘’)
{ $(this).val(orig_text); };
});

yes

For some reason I am getting an error on the top line. I am placing within <? ?>.

Any ideas why i am getting and error?

I would need to see the whole code as you have it. Plus what is the exact error?

sent private email to you.

You need to enclose the javascript inside

Can you check your private box.

What you sent me didn’t work. So I stayed up all night and finally i cracked my error. Here is what worked:

$(function(){
var orig_text = $(’#SearchTearm’).val();
$(’#SearchTearm’).focus(function(){
var current_text = $(this).val();
if(current_text != orig_text){}
else{ $(this).val(’’); }
}).focusout(function(){
var current_text = $(this).val();
if(current_text == ‘’)
{ $(this).val(orig_text);};
})
});

and then I placed it in a .js file. Then I loaded the .js file into my php code as an include. It Worked!

Thanks very much for your help!!! much appreciated.

That sounds like a better way than what you had. The code you sent was a pretty big mess honestly, so i tried to get it in there the best i could. But wasn’t really able to test it at all.

Yeah I’m still learning.

Got a Trick question.

How can I in the JS clear out the previous search.
For Example:

Some Text is in the search box, and when user clicks in the box to put new search term and hits search button on the return, the new search word + Some Text appears.

So how do i just have the user search term appear.

Thanks

Try changing these lines
[php]
if(isset($_POST[‘SearchTearm’])) {
$SearchTearm=$_POST[‘SearchTearm’];
}

[/php]
To this
[php]
$SearchTearm= (isset($_POST[‘SearchTearm’])) ? htmlentities($_POST[‘SearchTearm’]) : ‘Search’; // If POST isset then it uses that value else it uses the word “Search”

[/php]

:smiley: PERFECTO!!!

Thank YOU!!! If you have a paypal account send it to me, I will donate something to you for your help now, and future.

Sponsor our Newsletter | Privacy Policy | Terms of Service