Doing some research this is the code that worked for me but it is in the view and I would like to move to the application.js so I can use in different forms.
The other thing is that I have two scripts doing the same thing. One is for when I click on the checkbox and the other is to check if it is already checked when loading the page. Can I merge them so I don't have to repeat the code?
<script type="text/javascript">
function hideAddress()
{
if($('#same').is(":checked"))
$("#mailing-address").hide();
else
$("#mailing-address").show();
}
</script>
<script type="text/javascript">
$( document ).ready(function hideAddress() {
{
if ($('#same').is(":checked"))
$("#mailing-address").hide();
else
$("#mailing-address").show();
}
});
</script>
Edit
I tried moving to the application.js without the script tags, but now the .ready function doesn't work unless I reload the page.
$( document ).ready(hideAddress);
function hideAddress()
{
if($('#same').is(":checked"))
$("#mailing-address").hide();
else
$("#mailing-address").show();
}
So it looks like turbolinks makes document ready not work properly, so instead of (document).ready
I had to do $(document).on('turbolinks:load'
.
So i ended up setting this code on my application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
$(document).on('turbolinks:load', hideAddress );
function hideAddress()
{
if($('#same').is(":checked"))
$("#mailing-address").hide();
else
$("#mailing-address").show();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.