Abid Abid - 4 days ago 7
Javascript Question

If else not working in jquery

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.

Answer

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.. :)

Comments