4

Im trying to achieve the following:

A certain page will have a series of strings, which are replaced with database content if it exists.
For example:

<h2 class="label">Title:</h2>
<p class="value">{{Title}}</p>

Would become:

<h2 class="label">Title:</h2>
<p class="value">This is the title</p>

The problem is, if the database row for {{Title}} has never been entered, it displays the {{Title}} instead of replacing it with whitespace. So what id like to do is, with jquery, if .value contains {{, hide the whole element, in the same way display:none would.

Is this possible?

Thanks in advance.
Rob

3
  • 8
    Are you in control of the server-side? Sounds like the server-side logic just needs to be updated. Commented Oct 5, 2009 at 9:29
  • 1
    +1 with meder. If you do this with jQuery it will be visible for an instant. Better not showing it in first place using server-side processing. Commented Oct 5, 2009 at 9:32
  • 1
    The server side is a in house developed php CMS, unfortunately I'm not experienced enough in php (nor is time at an advantage at the moment) to tackle it there, although I do agree its how it should be resolved. I managed to fix another similar issue with jquery with the help of stackoverflow members a few months ago and although its a bit of a botch, it keeps the users happy while we figure out a long term solution, like moving the content over to a backend that we can actually manage :) Commented Oct 5, 2009 at 9:35

4 Answers 4

6
$(function () // when DOM is ready for manipulation, do:
{
    // for each of the p-elements in the DOM (add your own context or class-scope
    // or whatever you can do to narrow down the list of elements here):
    $("p").each(function () 
    {
        // cache the element (always a good idea when doing multiple operations
        // on the same entity):
        var that = $(this);

        // if the text of the element is "{{Title}}" then hide the element:
        if (that.text() == "{{Title}}") that.hide();

        // alternatively you can do:

        // if the the characters "{{" exists at any position in the text of the
        // element, hide it:
        if (that.text().indexOf("{{") > -1) that.hide();
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

You are welcome. My code looks a bit bulky, but you have to remember that JavaScript is a horribly slow language, where you really have to optimize for speed. Take [Kobi]'s answer--it is a lot cleaner than mine, but I suspect that it is also a lot slower on large collections.
Kobi has run some tests, and his code is about 2 times faster than mine. Even though my answer solved your problem, I suggest you accept his answer over mine, since its a lot slicker :)
@roosteronacid - I appreciate the juster, though it is unnecessary. Thanks.
Not at all, Kobi. This is about figuring out the best answer to a problem. And yours is the best answer.
5
$("p.value:contains('{{')").hide();

Edit:
There's a claim this code is slower. It should be said this is fairly basic, and in fact runs about 3 times faster.
Check this example (first one is slower): http://jsbin.com/ikite

12 Comments

Nice one. I'm curious though.. what is the performance-hit? It's not the most basic selector :)
@roosteronacid - I'd say it's pretty basic, I've done more complex selectors and had no problems.
@anonymous downvoder - please explain. The code works, btw: jsbin.com/oquto
I'm the one down voting. The selector is very clean and easily readable. But if you we're to run it on a collection of, say, a 100 elements, you would experience rendering-blips.
I stand corrected. Although my code runs a bit faster without the 2nd if-clause, your code is still about 2 times faster.
|
3

Try:

$('p.value').each(function(i,e){
  if ($(e).text().match(/^{{.*}}$/)) {
    $(e).hide();
  }
});

4 Comments

Just what I was looking for... a pitty I suck at regular expressions
-1 I see no need to new up a RegExp object each p-element. You can do this with a simple indexOf or equality-operation.
Also, you are not caching anything--if we're talking a large collection of elements, wrapping "this" (or in your case "e") multiple times is quite the performance-hit.
I second those, wasn't really optimized for performance. I'd go for the simpler indexOf if you don't need to be more valid.
0

Could you not just give the p a class of title?

<h2 class="title label">Title:</h2>
<p class="title value"></p>

And then to hide / show it:

if(title != '')
{
    $('p.title.value').text(title);
}
else
{
    $('.title').hide();
}

Comments

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.