I'm trying to swap .fa-eye to fa-eye-slash when user click on a button. What am I doing wrong? It's not working.
HTML code:
<button onclick="arata_ascunde(this);" style="align:right;font-size:13px"
class="btn btn-info " id="show_hide_bt" style="background-color:#00b0ff;"><i
class="fa fa-eye"></i> Show</button>
Javascript code:
function arata_ascunde(button) {
var x = document.getElementById('showhide');
var change = document.getElementById("show_hide_bt");
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
if (change.innerHTML == ' Show')
{
change.innerHTML = ' Hide';
$(button).find('i').toggleClass('fa-eye').toggleClass('fa-eye-slash');
}
else {
change.innerHTML = ' Show';
$(button).find('i').toggleClass('fa-eye-slash').toggleClass('fa-eye');
}
}
document.querySelector('button').addEventListener('click', function() {
const icon = this.querySelector('i');
const text = this.querySelector('span');
if (icon.classList.contains('fa-eye')) {
icon.classList.remove('fa-eye');
icon.classList.add('fa-eye-slash');
text.innerHTML = 'Hide';
} else {
icon.classList.remove('fa-eye-slash');
icon.classList.add('fa-eye');
text.innerHTML = 'Show';
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<button><i class="fa fa-eye"></i> <span>Show</span></button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With