Reputation: 129
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.
Upvotes: 0
Views: 115
Reputation: 1608
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.. :)
Upvotes: 4
Reputation: 2926
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.
Upvotes: 4
Reputation: 2093
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.
Upvotes: 3