简体   繁体   中英

How to know which id has been clicked in jquery?

I want to know that which button has been clicked in jquery. I have the html file:

<div class="row">
    <div class="col-md-6">
        <div class="well " id="option_a">
        </div>
    </div>
    <div class="col-md-6">
        <div class="well " id="option_b">
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <div class="well " id="option_c">
        </div>
    </div>
    <div class="col-md-6">
        <div class="well " id="option_d">
        </div>
    </div>
</div>

I need to know which option has been clicked?

Assuming you are binding event with element with class " well "

You can add code like following

$(".well").click(function(){
     var id = $(this).attr("id");
});

Here is my suggestion:

$('div.well').click(function() {
    console.log(this.id);
});

And here is the jsfiddle:

http://jsfiddle.net/k2Lhekpp/

There is alert instead of console.log and the div containers have red background color with width and height each 100px for better visualization of the clicks.

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