I am facing an issue in my Rails project where I am trying to toggle the visibility of an image when a user clicks on it. Below is the code snippet I have implemented:
$(document).ready(function() {
if ($("#myvideo").prop('muted', true)){
$("#mute").css("background-image","asset_url(mute.svg)");
}
$("#mute").click( function (){
if( $("#myvideo").prop('muted') ) {
$("#myvideo").prop('muted', false);
$("#mute").css("background-image","url(app/assets/icons/speaker.svg)");
} else {
$("#myvideo").prop('muted', true);
$("#mute").css("background-image","url('mute.svg')");
}
});
});
Despite trying multiple approaches, none of them seem to be working. However, using a direct URL like
$("#mute").css("background-image","url(https://www.svgrepo.com/show/170684/mute-volume-control.svg)");
works fine.
Edit: I also attempted the following:
$("#mute").css("background-image","url(<%= asset_path 'mute.svg' %>)");
Unfortunately, this did not yield the desired result. Can anyone shed light on why this might be happening and suggest a fix?