1

I want to hide strings inside a div. Consider the following example where I want to hide "some".

<div id="Disappear">Here is some text.</div>

After calling the function it should be

<div id="Disappear">Here is      text.</div>

Notice that the space remains reserved. Also the sentence and string to be hidden are random.

What have I tried?

Replacing the string with spaces. The solution is not elegant, since the width of the spaces can't always be equal to the string.

UPDATE I can't use <span> either. The actual div contains a lot more strings, using span is not an option.

9
  • 1
    make the string text color match the background color Commented Jul 20, 2012 at 9:56
  • 1
    Even inserting one on the fly? Commented Jul 20, 2012 at 9:58
  • 1
    No, if you want something hidden and still taking space you use the CSS visibility property. Its what its designed for. w3.org/TR/CSS2/visufx.html#propdef-visibility It will not render a box but will still consider it for layout purposes. And I suspect it does what you want better with accessibility considerations too (what if the user is ignoring your choice of colours?). Commented Jul 20, 2012 at 9:59
  • 2
    If you can't use span, you won't be able to hide text. You may try to replace the word with blank spaces, but the layout may change if you don't use a fixed width font. Commented Jul 20, 2012 at 10:01
  • 1
    Thanks all, paulslater19 example is what I wanted and what you all were suggesting. Its an elegant solution. Commented Jul 20, 2012 at 10:04

1 Answer 1

5

Here's a quick example - it wraps the search text in a span, which (through css) changes the visibility to hidden

http://jsfiddle.net/4kTVC/1/

Update - as mentioned in comments, {visibility: hidden} is better than {color: #fff}

Sign up to request clarification or add additional context in comments.

3 Comments

this is really nice and simple, great job!
I am not very good at using Regexs, can you modify it to only hide first matched string?
@AshwinSingh it's the "g", which means "global search" that was matching all of them. Here is an example with it removed: jsfiddle.net/4kTVC/2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.