tags:

views:

153

answers:

4

Hi, 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

+4  A: 
$(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();
    });
});
roosteronacid
Thanks a million!, this does just the trick.
prevailrob
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.
roosteronacid
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
@roosteronacid - I appreciate the juster, though it is unnecessary. Thanks.
Kobi
Not at all, Kobi. This is about figuring out the best answer to a problem. And yours _is_ the best answer.
roosteronacid
A: 

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();
}
smack0007
+3  A: 

Try:

$('p.value').each(function(i,e){
  if ($(e).text().match(/^{{.*}}$/)) {
    $(e).hide();
  }
});
MSX
Just what I was looking for... a pitty I suck at regular expressions
MaLKaV_eS
-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.
roosteronacid
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.
roosteronacid
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.
MSX
+5  A: 
$("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

Kobi
Nice one. I'm curious though.. what is the performance-hit? It's not the most basic selector :)
roosteronacid
@roosteronacid - I'd say it's pretty basic, I've done more complex selectors and had no problems.
Kobi
@anonymous downvoder - please explain. The code works, btw: http://jsbin.com/oquto
Kobi
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.
roosteronacid
You also have to consider other jQuery code running before/after your code.
roosteronacid
That's ridiculous. You're suggesting using only a subset of jQuery.
Kobi
Thats exactly what I'm suggesting. But I'll run some tests--It might just be that Sizzle translates your query into something along the lines of my logic. In which case I'd recommend your selector over my somewhat bulky code.
roosteronacid
Well, it's a shame you don't trust jQuery enough to use it properly. Please, make your experiments and hopefully you'll learn something. You'll find out reinventing the wheel, as they say, is never a good idea
Kobi
Could you try and use just one of the if-clauses?
roosteronacid
Good point. That makes it just twice slower :) Btw, you can try it yourself at http://jsbin.com/ikite/edit
Kobi
I stand corrected. Although my code runs a bit faster without the 2nd if-clause, your code is still about 2 times faster.
roosteronacid
@roosteronacid - it's over. We're cool, time to move to the next question. Good night!
Kobi