Support

Account

Home Forums General Issues If a field is empty (doesn\'t have any value), do something with plain Javascript

Solved

If a field is empty (doesn\'t have any value), do something with plain Javascript

  • I would like to have an H2 title “VIDEOS” and below that the results of the ACF field for every WooCommerce product.
    If one product doesn’t have any videos, i want to hide this H2 title (probably by adding a {display:none;} class.
    I want to do that using plain Javascript.
    Thanks in advance.

  • Why would you want to do this using JavaScript.

    I am assuming that you are conditionally showing the videos in PHP. Correct me if I’m wrong, but if this is the case then you could more easily conditionally show the H2 in PHP as well.

  • I don’t mind using PHP, i am just not familiar with the language.
    Could you provide me with a code snippet?

    I am imagining something like this in JS:

    if( field_name = ‘ ‘){
    document.querySelector(‘.video–title’).classList.add(‘hidden’)
    }

    ———-
    CSS

    .hidden {
    display:none;}

    ———–

    For the JS part, I don’t know how to implement that the field returns nothing. Probably the way i wrote the code is wrong.

    I would appreciate your help, even in PHP.

  • I do not know what types of fields you are using, so it is very hard to give code. The basic idea is

    
    if (condition to check for videos) {
      // output heading field
      // output videos
    }
    
  • Let’s say it’s a video field. Or an image field. Or a Wysiwyg editor. I actually have all of them, but my main concern is how to implement in code that the field might be empty. PHP or JS. I don’t mind.

    The screenshot will help.

    Thanks for your time.

  • 
    <?php 
    if (get_field('video')) {
      ?>
        <h2>Videos</h3>
      <?php 
      the_field('video');
    }
    ?>
    
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.