G Naga Subrahmanyam G Naga Subrahmanyam - 6 months ago 20
jQuery Question

like button not working from aritical 2

Like button not working from article 2



Note: Like button works well for Article 1.

Article 2 like button is not toggled.
I am using toggleClass to toogle fontawesome icon.
The code is given below.

Please help me to solve this problem.



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Like Button</title>

<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<style type="text/css">
.well{
background-color: white;
}
body{
background-color: #f7f7f7;
}
.love{
font-size: 20px ! important;
}
.love:hover{
cursor: pointer;
}
.fa-heart-o{
color: red;
}
.fa-heart{
color: red;
}
</style>
</head>
<body>
<div class="container">
<h3>
Like button not working from Artical 2
</h3>
<div class="well"><h4>Artical 1</h4><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore, qui aspernatur accusantium! Molestiae, cum cupiditate nam optio dignissimos magnam velit, perspiciatis amet qui aut nobis, quisquam, laudantium vitae eos ipsam.</p>
<hr>
<span id="love" class="love"><i class="fa fa-heart-o" aria-hidden="true"></i> Like</span>
</div>

<div class="well"><h4>Artical 2</h4><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore, qui aspernatur accusantium! Molestiae, cum cupiditate nam optio dignissimos magnam velit, perspiciatis amet qui aut nobis, quisquam, laudantium vitae eos ipsam.</p>
<hr>
<span id="love" class="love"><i class="fa fa-heart-o" aria-hidden="true"></i> Like</span></div>

<div class="well"><h4>Artical 3</h4><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore, qui aspernatur accusantium! Molestiae, cum cupiditate nam optio dignissimos magnam velit, perspiciatis amet qui aut nobis, quisquam, laudantium vitae eos ipsam.</p>
<hr>
<span id="love" class="love"><i class="fa fa-heart-o" aria-hidden="true"></i> Like</span></div>

<div class="well"><h4>Artical 4</h4><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore, qui aspernatur accusantium! Mo2estiae, cum cupiditate nam optio dignissimos magnam velit, perspiciatis amet qui aut nobis, quisquam, laudantium vitae eos ipsam.</p>
<hr>
<span id="love" class="love"><i class="fa fa-heart-o" aria-hidden="true"></i> Like</span></div>

<div class="well"><h4>Artical 5</h4><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore, qui aspernatur accusantium! Mo2estiae, cum cupiditate nam optio dignissimos magnam velit, perspiciatis amet qui aut nobis, quisquam, laudantium vitae eos ipsam.</p>
<hr>
<span id="love" class="love"><i class="fa fa-heart-o" aria-hidden="true"></i> Like</span></div>
</div>

<!-- jQuery (necessary f2r Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#love").click(function(){

$(this).find('i').toggleClass('fa-heart-o fa-heart');
});
});
</script>
</body>
</html>




Answer

Just a little change in your script Use class and not id and it works fine

<script type="text/javascript">
    $(document).ready(function(){
        $(".love").click(function(){
            $(this).find('i').toggleClass('fa-heart-o fa-heart');
        });
    });
</script>