i have a form with a jquery script that show/hide some divs (private form content and company form content). When i view my page, i can choose between private forms and company forms with radio buttons. but, if i view the page with an id (e.g. mypage?id=1), the content will not show until i click on the right radio button.
Here is my code:
$(function () {
$('#company_fields').hide();
$('#private_fields').hide();
$("input[name=art]:radio").change(function () {
if ($('input[name=art]:checked').val() == "company") {
$('#private_fields').hide();
$('#company_fields').show();
} else if ($('input[name=art]:checked').val() == "private") {
$('#private_fields').show();
$('#company_fields').hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<form>
<label for="company" class="radio-inline">
<input type="radio" name="art" id="comp_yes" value="private"> private
</label>
<label for="company" class="radio-inline">
<input type="radio" name="art" id="comp_no" value="company"> company
</label>
</form>
<div id="private_fields">
<div class="form-group">
<label for="textinput" class="control-label">Street</label>
<input type="text" class="form-control" name="street" id="street" placeholder="">
</div>
</div>
<div id="company_fields">
<div class="form-group">
<label for="textinput" class="control-label">Company</label>
<input type="text" class="form-control" name="company" id="company" placeholder="">
</div>
</div>
My input for the radio buttons look like this (the code editor dont understood php):
<input type="radio" name="art" id="comp_no" value="private" <?php if (isset($_GET['id'])) {echo $row->art == "private" ? 'checked="checked"' : "";}?>> private
i hope anyone can help me that the content show until i view my page with an id, too. The right radio button is checked, but the content will not show up.. everything is wrong with my query code :/
sorry for bad english..
</div>
The change event is not triggered while the page is loading. You could trigger the event manually after you defined it.
$(function () {
$('#company_fields').hide();
$('#private_fields').hide();
$("input[name=art]:radio").change(function () {
if ($('input[name=art]:checked').val() == "company") {
$('#private_fields').hide();
$('#company_fields').show();
} else if ($('input[name=art]:checked').val() == "private") {
$('#private_fields').show();
$('#company_fields').hide();
}
}).trigger('change');
});
Or you could directly hide/show the div by PHP.