user2837485
user2837485

Reputation: 59

targeting one button of same id

all i have li elements.In all li elements i have a button with same id.So what i am trying to do is that when i click that button in any li element, the text of that button should change

       <ul>
           <li><span class="yes_button"></span></li>
           <li><span class="yes_button"></span></li>
           <li><span class="yes_button"></span></li>
           <li><span class="yes_button"></span></li>
       </ul>

       $(".yes_button").click(function(){
         $(".yes_button").text("no");        
       })

the problem i am having is that when click any button the text changes in all buttons present in different li elements

Upvotes: 0

Views: 107

Answers (2)

Alex Char
Alex Char

Reputation: 33218

Another solution is:

$(".yes_button").click(function() {
  $(this).text(function(i, text) {
    return text === "yes" ? "no" : "yes";
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li><span class="yes_button">yes</span></li>
  <li><span class="yes_button">yes</span></li>
  <li><span class="yes_button">yes</span></li>
  <li><span class="yes_button">yes</span></li>
</ul>

Upvotes: 2

Vitorino fernandes
Vitorino fernandes

Reputation: 15951

use this

 $(".yes_button").click(function(){
     $(this).text("no");        
  })

Upvotes: 0

Related Questions