A simple if statement seems to mess one function <p>I´m trying to hide and show some of the input fields in a form with jQuery. But it seems as the first if statement in the is messing things up. If I comment the if statement, everything works like a charm (except to check the default radio button if old input is missing).</p> <p>The line <code>{{ old('type_of_content') }}</code> is from the Laravel Framework to retrieve old form input data if the validation fails.</p> <pre><code>$(document).ready(function(){ // Set the checked radiobutton var radio_checked = '{{ old('type_of_content') }}'; if (radio_checked) { // check if variable is empty or not $('input:radio[id='+radio_checked+']').attr('checked', true); }else{ $('input:radio[id=is_question]').attr('checked', true); } $.fn.showQuestion = function(){ $("#remove_questions_choices").show(); $("#remove_questions_box").show(); $("#data").show(); // Fråga $("#information_body").hide(); $("#is_information").attr("checked", false) $("#is_question").attr("checked", true) alert('Question is selected') } $.fn.showInformation = function(){ $("#remove_questions_choices").hide(); $("#remove_questions_box").hide(); $("#data").hide(); $("#information_body").show(); $("#is_question").attr("checked", false) $("#is_information").attr("checked", true) alert('Information is selected') } // If radio button is changed by user click $('input:radio[name=type_of_content]').change(function () { if ($("#is_question:checked").val()) { $.fn.showQuestion(); } if ($("#is_information:checked").val()) { $.fn.showInformation(); } }); // When a radio button is selected on page load (working) $('#is_question:checked').val(function(){ $.fn.showQuestion(); }); $('#is_information:checked').val(function(){ $.fn.showInformation(); }); }); </code></pre> <p>The expected result should be to check if old input data exist, and check the corresponding radio button if so. Else, check the "default" radio button. No errors is reported in the chrome dev tools.</p> <p>But what is happening is that i cannot "reselect" the <code>#is_question</code>, the <code>showQuestion()</code>. But <code>showInformation()</code> seems to be working.</p> <p>Using jQuery 3.1.0</p> <p>Made a fiddle: <a href="https://jsfiddle.net/zs85p7nx/" rel="nofollow noreferrer">https://jsfiddle.net/zs85p7nx/</a></p>