简体   繁体   中英

How do i target a unique id of a button

My goal is to target unique id of a button using jquery.

For example

<button id="5412313">Remove</button>
<button id="9882813">Remove</button>
<button id="123123123">Remove</button>
<button id="214343">Remove</button>
<button id="3434343">Remove</button>
<button id="4343434">Remove</button>

Jquery

$("#WhatshouldIputInHere?").on("click", function(){
  console.log($(this));
});

You can try with remove() .

$("button").on("click", function(){
 $(this).remove(); 
 //$(this).attr('id');
});

Based on your button label i came to know this.you can do anything with your id which i have commanded .

You can use event object in the handler function, the code is like this:

$('button').on('click', function(event) {
    alert(event.target.id);
})

Here's the effect of the code above

$('button').on('click',function(){
    console.log($(this));
    if($(this).attr('id')=='9882813'){
        console.log('button with id '+$(this).attr('id')+'clicked');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM