These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Worked myself into a corner: Self-Clearing search field.

DTjava
Registered: Jun 17 2011
Posts: 21
Answered

Hi everyone,
 
I'm building a custom search field, and I'm trying to solve the issue of the text in the search field being searchable. Since the search field is on every page, the customer will get a lot of unwanted returns.
 
My solution was to blank the field using a custom script and "on blur" command.
 
This went in custom format for the text field:
 
if( (event.commitKey == 2)&& !/^\s*$/.test(event.value))search.query(event.value, "ActiveDoc");
event.value = " ";
 
And then I added a "clear field" on blur command - This reset the form before the search engaged, allowing it to proceed normally. It properly searches the document bypassing the search field, which is now empty. This all works when a user pushes the "enter" key.
 
This is all well and good, but I also have a search button that engages the query from the text field (it also clears the field after it runs with a "mouse up" action, duplicating the fix above).
 
The problem? Since I've got the "On Blur" Action on the text field, every time you click the search button it clears the search field before the query can run. So it searches nothing.
 
Is there a way I can keep the data being entered into the search field (perhaps in a hidden text field) so that my search button can call on THAT field instead? This would solve pretty much everything. But I can't figure out how to get that hidden text field to keep its data. Is there a way for it to hang onto the data beofre the event.value = " " blasts it?
 
Phew - Hope everyone followed that. Thanks!

My Product Information:
Acrobat Pro 9.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
If you want to clear the field you need to do it on the will commit event, i.e. the keystroke event

if(event.willcommit)
{
... your code ...
}

This way the value of the field is never set.

copy the data to a document level variable. The data is then accessible to all scripts on the document.

this.MySearchString = event.value;




Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

DTjava
Registered: Jun 17 2011
Posts: 21
Accepted Answer
Hi Thom,

Thanks so much for your answer! I see what you were saying. I've actually created a work around.

It is NOT pretty, but it works. It's actually a combination of javascript (including some choice advice from your earlier posts), actions and a hidden text field.

If anyone is interested in how I created this custom search field and button:

Create a text field called "SearchField1". This is your search input field - Add a custom script:

var TextBank = getField("TextBank");
{
if( (event.commitKey == 2)&& !/^\s*$/.test(event.value))search.query(event.value, "ActiveDoc");
TextBank.value = event.value;
event.value = " ";
resetForm(["SearchField1"]);
}

Add a "Search Button" next to your text field and add a mouse up action that executes a javascript:

var Searchword = this.getField("TextBank").value;
search.bookmarks = "false";
search.wordMatching = "MatchAllWords";
search.query(Searchword, "ActiveDoc");

Finally, add a hidden, read only text field tucked away in a corner somewhere called "TextBank."

So what's happening up there?

The search field does the main search. In order for it not to search itself, it wipes out it's own data before execution - But not before populating that hidden text field called TextBank.

My problem was that the search button couldn't call up data from the search field, since it wiped itself out (on purpose).

So, we created TextBank that holds onto the initial search query before it got wiped. The search button references this hidden field instead of the search field.

Ugly, but it'll fly.

Thanks!


D.