tags:

views:

373

answers:

2

Hi, this is my first time posting here, I have a question which I have searched high and low for and so far cannot find a working answer.

The HTML would be as follows:

<span class="label">Label:</span>
<span class="value">{{Value}}</span>

Basically, {{Value}} is a variable inserted (or not) via php/mysql.
Of course if {{Value}} is empty, you get the following when the page is rendered:

<span class="label">Label:</span>
<span class="value"></span>

The idea is for Label: to be hidden if there is nothing in {{Value}}.

I have tried various examples using $(.value:empty) & if statements but it hides .label regardless.

Any suggestions are welcome.
Thanks in advance,
Rob

A: 

Try

if ($('.value').text() == '') { $('.label').hide(); }
Tomas Lycken
Will that work if the page has multiple .value / .label pairs?
Thomas L Holaday
Thanks. Tried this one though and it still shows .label :(
Rob, as Thomas points out this will only work if you have _one_ value/label pair - with multiple pairs, if there is one .value span that isn't empty, the condition will not be met. Do you have any other property - id or something - on the html elements that can be used to identify them? Where are they in the DOM tree?
Tomas Lycken
+3  A: 
$(".value").each(function(){
  var value = $.trim($(this).text())
  if(value == ""){
    $(this).prev(".label").hide()
  }
})

PS. A good idea will be to encapsulate the .label .value pair into some sort of LI or DIV

duckyflip
For my own instruction, the trim($this) deals with the case where there's only whitespace in the span?
Thomas L Holaday
Thomas that's right, I presume it's why your original :empty selector did not work.
duckyflip
Thanks a million. I edited the markup to make sure the element with the .label class was immediately before .value (there were other tags in between which were causing it not to work..) and it works a charm.Thanks very much duckyflip and thanks to all those that answered.Rob