admin管理员组

文章数量:1290983

I have a two buttons with the same selector class. When I do this:

$('.my_button').click(function() {
    console.log(1);
});

, and then click on the button it log 1 two times, like I clicked both buttons instead single. So my question is: There exists some way in JS to get only that button what I clicked, without assign unique selector like id. I am newbien in JS, so can somebody explain me? I found related issue here. Thanks!

Edit:

I make buttons a little bit different. And yes, it returns only single button, but why click trigger works two times. Console log log two times.

I have a two buttons with the same selector class. When I do this:

$('.my_button').click(function() {
    console.log(1);
});

, and then click on the button it log 1 two times, like I clicked both buttons instead single. So my question is: There exists some way in JS to get only that button what I clicked, without assign unique selector like id. I am newbien in JS, so can somebody explain me? I found related issue here. Thanks!

Edit:

I make buttons a little bit different. And yes, it returns only single button, but why click trigger works two times. Console log log two times.

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Mar 5, 2015 at 8:29 nowikonowiko 2,5677 gold badges40 silver badges87 bronze badges 5
  • 1 why don't you use id instead of class – Optimus Commented Mar 5, 2015 at 8:33
  • @Optimus, I redeclare Symfony2 form theming, and for some reasons I cant override id, but I can append class to button theme. – nowiko Commented Mar 5, 2015 at 8:45
  • can you add name property to this button – Optimus Commented Mar 5, 2015 at 8:49
  • is there any difference between this two button like value, name or anything – Optimus Commented Mar 5, 2015 at 8:50
  • @Optimus, no they identical – nowiko Commented Mar 5, 2015 at 9:28
Add a ment  | 

6 Answers 6

Reset to default 4

Every event listener receives the event, which carries the event target. Try the below example.

$('.my_button').click(function(e) {
    console.log(e);
    console.log(e.currentTarget);
    console.log($(e.currentTarget));
});

use this inside your function code

$('.my_button').on('click',function() {
    var tempContainer=$(this).parent();
    alert($(tempContainer).html());   // you ll see that you are showing the code where exists your clicked button
});

Assign different id to your buttons

$(".my_button").on("click",function(){
  console.log($(this).attr("id"))
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="button" class="my_button" id="test" value="Test"/>
<input type="button" class="my_button" id="test2" value="Test-2"/>

Try this:

<button class="my_button">Content1</button>
<button class="my_button">Content2</button>

<script>
$( ".my_button" ).click(function( event ) {
console.log(1);
});
</script>

https://jsfiddle/nt9ryeyr/5/

Try this:-

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(".p").click(function(e){
        alert($(e.currentTarget).attr("value"));//button text on which you clicked
    });
});
</script>
</head>
<body>

<input type='button' class="p" value='test'/>

</body>
</html>

if your html is like this

<button class="my_button">Test</button>
<button class="my_button">Test1</button>

then use this script

$('.my_button').click(function() {
if(this.innerHTML ==="Test")
    console.log(1);
else
console.log(2);
});

or if your html is like this

<input type="button" class="my_button" value="Test"/>
<input type="button" class="my_button" value="Test1"/>

then use this script

$('.my_button').click(function() {
if($(this).val() ==="Test")
    console.log(1);
else
console.log(2);
});

本文标签: