View Full Version : Javascript revealer

11-16-2010, 05:38 PM

I'm quite new to Javascript but I decided to learn it.
I want to make a function but I can't get hold of the right stuff to do. That's why I ask for your help!

So I have got a <div> with many hidden <p>s. I want to make my script so that when a form input (text) is being written by the user (so onKeyUp), it reveals those paragraphs which have got the kind of text in them that's in the form input box.

So for example:
- the box is empty -> all paragraphs are hidden
- the box has got "a" in it -> the javascript searches all paragraphs and makes those visible which paragraphs have the letter 'a' in them anywhere, and leaves the other paragraphs hidden.
- the box has got "are" in it -> the script (or the function) searches the paragraph for the word "are" and shows those whcih have got it in them.

I know how to make a paragrah hidden and visible, I also know how to set up the event with the form box to call the function...

My problem is that I don't know how to et up the search in the particular paragraphs and how to get their ID perhaps... Once I had their ID I could make them visible.

So my question is... Is there a way to search text in particular paragrah and if the search positive get their IDs? Or is it possible to search for IDs?

Philip M
11-16-2010, 07:41 PM
It is quite remarkable how beginners always seem to want the oddest and most outlandish things. :)

I see many problems here. The user types "a" and all the paras containing the letter a (such as along) are revealed. He then types "r", and at once all those paras which do not contain "ar" are hidden again.

And do you mean whole words or embedded words? If I type "fat" do I get "father"? Which is then hidden again when I go on to type "fatter"? Sounds a total mess to me. :eek:

I think you should think this through and ask yourself why you want to do this. It will certainly drive people away from your site. If you are trying to devise some sort of search feature, then that should be done server-side.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Old Pedant
11-16-2010, 08:14 PM
FWIW, if you still MUST do what you described, it's nearly trivial.

11-16-2010, 08:19 PM
javascript autocomplete