Javascript searching a text area...

He has: 1,758 posts

Joined: Jul 2002

Hey guys,

A client has recently come to me with a request and it's kinda blown me away. I'm not too hot with the old javascript and from what he's said it's going to be way over my level of understanding (I always prefer to do things server side rather than client side).

Basically we have a CMS system where the client can go in and edit pages, however some of the textarea fields have a lot of text in and from time to time he has to go in and find certain parts.

What he wants to do is have a search box above the text area and when he enters a string it will search the content of the textarea and highlight the word (as if it had been double clicked) and bring the searched text to the top of the box (and then if he searches for the same word again, find the next one and so on and so forth, looping back to the top if neccessary).

So... what I'm wondering is... is it possible? I can't seem to find any pre-written javascript that will achieve this, so you guys are kinda my last resort!

I hope you can help!

Andy

Andy

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

It must be possible. I haven't seen anything implemented just like this, but parts of what you want exist, I think.

Is it a or a dhtml text area (iframe, div or such)?

He has: 1,758 posts

Joined: Jul 2002

It's a proper html form textarea input field. If it was text loading in an Iframe I think it would be relativly easy (cos you could just do it serverside with php) but I havent got a clue with the text area.

Andy

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Alright, I couldn't resist writing up a script for this. I've got it to the point where it locates the first occurance of a string and derives the starting and ending co-ords of that. But I've yet to get the thing to select. And I haven't got a "find next" function either. So I won't bother putting it up just yet. Smiling Soon as I figure out at least the selection bit, I'll have you a Moz-only sample.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

I've got this far tonight: http://www.sitebug.com/abhi/TWF/andy_js.html
JS: http://www.sitebug.com/abhi/TWF/andy_select.js
(code is a mess and comments aren't updated)

Essentially, it works. The only crucial part missing is the highlighting itself... which I haven't quite figured out yet. Sticking out tongue

There are lots of details needing attention like case-sensitivity toggling and such. Do you want regex in it too? Confused

I'll dig deeper tomorrow. Smiling

s0da's picture

He has: 157 posts

Joined: Mar 2004

this could be quite useful. mind if i take a jab at it?

wow, this highlighting is playing mind games on me.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

s0da wrote: this could be quite useful. mind if i take a jab at it?

wow, this highlighting is playing mind games on me.

Asking me? Sure, do what you will with it... Smiling

Highlighting is a pain for me. I've avoided ranges because of the differences between the IE and Moz models. I'm trying to implement the Moz form first, though. But I'm not sure if I can at all have it select text in a . Sad

He has: 1,380 posts

Joined: Feb 2002

output the text to a printed text, inside tags or something, with the specified words in a different color band or something, and then have it output on button click back to the form with all instances of the word replaced with [selected] or something...The main point of this [selected], is that the [selected] area is...'

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Or something. Smiling

That's a futile solution because ultimately the text needs to be found by the user... simply replacing with [selected] won't display the location of the text. Sad

s0da's picture

He has: 157 posts

Joined: Mar 2004

i been at this like all day. i really do think it can't be done at all in a textarea. it would be quite useful too. *sigh*

i found something searching google: (first result)
http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=textarea+select+range

ima sleep nowwww. kweeeeeeeeeeeeeee

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

heh, I'm still hacking away at it... I can't get my head around how ranges work in the first place. Mostly because of all the conflicting documentation for each browser. Sad Maybe I should try getting it working on an ordinary node like a p or div before tackling textarea...

hm, that result looks interesting...

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Success! Well, mostly. Smiling http://www.sitebug.com/abhi/TWF/andy_js.html
JS: http://www.sitebug.com/abhi/TWF/andy_select.js

It highlights the text properly in Moz. Missing scroll-to functionality (working on it now), and has a problem with losing focus sometimes. Just needs some refining. Smiling

Thanks to s0da -- setSelectionRange(start, end) is the function we needed, which is used in that last link. Wink

He has: 1,758 posts

Joined: Jul 2002

WOW! That is amazing you guys! I'm really impressed! Looking really good so far.

Do you plan to make it work in IE as well or will I have to use Mozilla?

Thanks for all your efforts, I really *really* appreciate this.

Andy

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Yeah, I'll IE-fy it after getting it fully functional in Moz. I usually keep the uglification (IE code) for last. Sticking out tongue

They have: 5,633 posts

Joined: Jan 1970

You cannot highlight a single word in a text area. You must highlight everything with in a tag. The only way is to...... well its hard to explain. It can be done but it will be a very large script and Will not look that good. Also if the textarea will var then it will be extreamly hard. Start thinking out side the tag now. Might want to consider flash or java. Sorry too complex for me to do for free.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

bja888 wrote: You cannot highlight a single word in a text area. You must highlight everything with in a tag. The only way is to...... well its hard to explain. It can be done but it will be a very large script and Will not look that good. Also if the textarea will var then it will be extreamly hard. Start thinking out side the tag now. Might want to consider flash or java. Sorry too complex for me to do for free.

I disagree. Visit the page I linked to above using a recent Mozilla browser and you'll see it works. setSelectRange(start,end) highlights a string in a textarea from start offset to end offset.*

Why will it matter if the text varies (especially in this script)? If a match exists in the textarea (indexOf() >= 0), then the contents of the textarea are virtually static. With simple validation and dependence on textarea.value.length(), I don't see why it would pose a problem.

The script I've written is relatively simple. It's not even that large...

* The only problem with setSelectRange() is that it's undocumented, so I don't know how well it works with older versions. Don't know if it functions in IE either.

---

s0da, what've you got so far? What's the trouble with Find Next? I store matches in an array, and iterate through them when Find Next/Prev is called.

s0da's picture

He has: 157 posts

Joined: Mar 2004

i was spanking this script up in IE. good to know the highlight was tackled in moz. Laughing out loud

having trouble with finding next... if i can only get this then searching backwards would be a sinch. Sad

i'm also trying to do a Go To Line.. thingy.

They have: 5,633 posts

Joined: Jan 1970

Well if this setSelectRange() exists it would be the only way to write the script. Everything other than that is easy. Come on I could do it in 2 days.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

bja888 wrote: Well if this setSelectRange() exists it would be the only way to write the script. Everything other than that is easy. Come on I could do it in 2 days.

If it exists? It does exist. Once again I point you to the scripts linked to in previous posts. Smiling The only trouble is it isn't documented officially.

Feel free to proceed with writing a solution if wish.

Edit: it's setSelectionRange(), sorry, as I mention above...

s0da's picture

He has: 157 posts

Joined: Mar 2004

ah! i get it. i really hate arrays... i don't even like them in C++. scary stuff

i have find, find what: replace with: and now i'm going to work on find next once i'm done with this other script.

oh and works in IE and Firefox. Wink

He has: 1,758 posts

Joined: Jul 2002

I'm really looking forward to seeing these scripts working in IE. I think it's a gonna be a brilliant bit of code that will be useful to loads of other people not just me. I'm really greatful to you guys for putting the effort in!

Andy

He has: 1,380 posts

Joined: Feb 2002

thats awesome...collaboration at it's best

you can even call that Open Source....lol

s0da's picture

He has: 157 posts

Joined: Mar 2004

hmm... the case aint matchin correctly. i'll have to put a switch in for matching case or no match case.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Doesn't work in Safari... Wink

Works in FireFox, though! The replace didn't work, but the replace all did.

VERY nicely done, despite the nay-sayer in the middle even!

s0da's picture

He has: 157 posts

Joined: Mar 2004

it's kinda buggy right now; this disappoints me. i'll have to work on it a bit more. Sad

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Brilliant! Nice work. Laughing out loud

Suggestion: could also have a "wrap" toggle (or perhaps instead of "up" and "down"), like the Moz browsers do. If wrap is on, it'll search down until the end, and start again from the top. That way you don't limit yourself to only one portion of the text. Or you do if you want to. Wink

He has: 1,758 posts

Joined: Jul 2002

Very VERY nice... I'm impressed guys! Let us know when you've fixed the bugs! I hope your going to submit the script to some javascript sites and share it with the world! Smiling

Andy

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.