Error typing IF and Else in jquery

Asked

Viewed 1,838 times

1

I’m starting to venture now into the world of development and hit a question while using IF and Else.

The code below is OK

    <select id="course_list" >
      <?php $my_query = new WP_Query('tribe_events_cat=petroleo-e-gas'); ?>
         <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <option value="course_<?php the_ID(); ?>" >
                <?php the_title(); ?>
            </option>
        <?php endwhile; ?>
    </select>

<script type="text/javascript">
$("#course_list").on('change', function(){
        $('.course_item').hide();
        $('#' + this.value).show();

    });
</script>

But when I try to improve the code and to re-show all the courses, everything stopped working.

<select id="course_list" >
        <?php $my_query = new WP_Query('tribe_events_cat=petroleo-e-gas'); ?>
        <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <option value="all-posts" >
            Mostrar Todos
        </option>
        <option value="course_<?php the_ID(); ?>" >
            <?php the_title(); ?>
        </option>
        <?php endwhile; ?>
    </select>
    <script type="text/javascript">
    $("#course_list").on('change', function(){

        if {
            $('.course_item').hide();
            $('#' + this.value).show();
           }

        else

            $('.course_item=').show();


        });
    </script>
  • 1

    What do you want to check on if? the if must have a condition to check within parentheses: if (algo a verificar){ // fazer algo

1 answer

1

Hello!

Your code has a syntax error in the "if Else" block. To correct you will need to put a condition to be tested inside if. For example:

if(ALGUMA_VARIAVEL == CONDICAO) {
    $('.course_item').hide();
    $('#' + this.value).show();
} else
    $('.course_item=').show();

If the condition is met at some point, your 'if' block will be executed, otherwise the 'Else' will always be executed'.

  • Sergio thanks for the tips the text was great and Lucas will try to make the code work later, but thank you so much for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.