0

I have several variables in my code :

var     titre = bloc_movie_parent.attr("titre"),
    director = bloc_movie_parent.attr("director"),
    prod = bloc_movie_parent.attr("prod"),
    agency = bloc_movie_parent.attr("agency");

using this variables i'm building a div using a for each function, and inside this div I have spans that should show but only if the variable exists.

here is my code :

        $(".bloc_movie[type='"+ type + "']").each(function(){

            var titre = $(this).attr("titre"),
            director = $(this).attr("director"),
            prod = $(this).attr("prod"),
            agency = $(this).attr("agency"),
            hash = $(this).children('a').attr("href");

            $(".caption_slideshow").append('<div class="bloc_movie"><div class="legende"><div class="titre_caption">'+ titre +'</div><div><span class="initial_director">D/ </span>'+ director +'</div><div><span class="initial_prod">P/ </span>'+ prod +'</div><div><span class="initial_agency">A/ </span>'+ agency +'</div></div></div>');});

it works fine.

but sometimes my variable is empty. And when the variable is empty, I want the span to be hidden. for example if "director" is empty, I want the "initial_director" span to be hidden. same for "prod" with "initial_prod" span & "agency" with "initial_agency".

I can't find a way of doing it inside the append function.

can anybody help me with this ?

4
  • which variable do you mean when "my variable is empty " ? and which span do you want to hide explain please Commented May 11, 2017 at 14:21
  • what about appending them one by one so that you can check them individually before each append Commented May 11, 2017 at 14:23
  • @FadiAboMsalam for example if "director" is empty, I want the "initial_director" span to be hidden. same for "prod" with "initial_prod" span & "agency" with "initial_agency". Commented May 11, 2017 at 14:26
  • @Matt I can't append them individually because my divs are built after an onclick function, they are not built on the document.ready function Commented May 11, 2017 at 14:28

1 Answer 1

1

first define css class called .display-none{ display:none;} then the main idea is

if(!director){$('.initial_director').addClass("display-none")}

one way of doing it like this adding always a class of display-none concatinated with the value of you variable like this "display-none"+director here we have two cases the first is empty then the result is display-none which will hide your div the second case will have a value so the result would be display-nonevalue which doesn't add any styles

<div><span class="initial_prod display-none"+prod+"\"">P/ </span>'+ prod +'</div>
Sign up to request clarification or add additional context in comments.

3 Comments

it's not working as its not looping my for each function. If one of my agency var is empty, then all the span are getting the "disply-none" class
nice trick ! I had to adapt the code a bit but it works like a charm now ! thanks a lot
@mmdwc my pleasure :)

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.