0

I am facing a small issue with jquery code below.The first if segment of the code is working fine but the else part is not working.

function show_table() {
        var table_name = '<?php echo $table_name; ?>';
        if(table_name = "users" ) {
            $("#users").css('display', 'block');
        }else if(table_name = "questions") {
            $("#questions").css('display', 'block');

        }
    }


  $(document).ready(function() {
        $("#block").click(function() {
            show_table();
            return false;
        });
    });

<a id="block" class="btn btn-info fa fa-cog" href=""></a>

<div style="display: none;" id="questions">
   <p>Questions</p>
</div>

<div style="display: none;" id="users">
   <p>users</p>
</div>

Can any one please help me solve this issue.

1
  • table_name = "questions" to table_name == "questions" Commented Nov 30, 2016 at 13:12

3 Answers 3

4

This error must be because you are assigning the variable table_name with the string "users" rather than checking weather equal to it, in the if condition. i.e, you have a small typo error at the if condition statement if(table_name = "users" ) in the third line of your code.

change it like if(table_name == "users" ) and the code is bound to work.

not only on the third line's if condition, its repeated regularly in the if condition. replace the = with == in all the if conditions.

hope this helps and the code works.. :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thnx how could i do that :)
search for if( and check weather if its a = or a ==. There are very less possibilities of an assignment inside an if.
4

Use == NOT = in the if and else if condition , using = will always result true and so the if condition will always be executed unless the assign is to a 0 or NaN or undefined or other falsy values.

1 Comment

hey what's wrong in your explanation that OP didn't accept this ??
3

Use == instead of =. The former is a comparison, while the latter is an assignment.

    if (table_name == "users" ) {
        $("#users").css('display', 'block');
    } else if(table_name == "questions") {
        $("#questions").css('display', 'block');
    }

Read more in the documentation.

Comments

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.