0
<div id="url" class="infoline">
  <i class="fa fa-link fa-2x fa-fw coloriconfa"></i>
  <a href="<?=$data['post']['url']?>" target="_blank"><?=$data['post']['url']?></a>                     
</div>

Hide searches with this function JS

$(document).ready(function() {
$("#url").each(function() {

    if($.trim($(this).html()).length == '') {
       $(this).hide();
    };
 });
});

Help me please

ShvetZz
  • 1
  • 1
  • 1
    You're checking `.length` to an empty string, should be `.length == 0` – Phix Feb 03 '22 at 17:59
  • I think you want `.text()`, not `.html()`. `.html()` will include all the nested tags. – Barmar Feb 03 '22 at 17:59
  • 1
    There's no need to use `.each()`. IDs have to be unique, so there's nothing to loop over.\ – Barmar Feb 03 '22 at 18:00
  • I want to hide the whole
    with id="url" if it doesn't contain any data
    – ShvetZz Feb 03 '22 at 18:04
  • 1
    @ShvetZz then you need to check if the `a` tag in the div is empty, not the entire `#url` div, which has children. – Phix Feb 03 '22 at 18:06
  • @Phix Yes, you are right) Everything turned out! Here's the resulting code: '$("#ur").each(function() { if($.trim($(ur).html()).length == 0) { $(url).hide(); }; });' – ShvetZz Feb 03 '22 at 18:14
  • To clarify, [jQuery ID selector works only for the first element](https://stackoverflow.com/questions/11114622/jquery-id-selector-works-only-for-the-first-element). – isherwood Feb 04 '22 at 16:11

1 Answers1

0

A friend @Phix suggested a solution and it worked!

<div id="url" class="infoline">
  <i class="fa fa-link fa-2x fa-fw coloriconfa"></i>
  <a id="ur" href="<?=$data['post']['url']?>" target="_blank"><?=$data['post']['url']?></a>                     
</div>

Tweaked

$(document).ready(function() {


    if($.trim($(ur).html()).length == 0) {
       $(url).hide();
    };
});

I had to add an id for line a, but this is tolerable)

ShvetZz
  • 1
  • 1