Strannik
Strannik

Reputation: 466

how do i know id previous element

how do i know id checkbox when you click on <span class="CheckBox">?

Code:

<div id="checkbox">
<input type="checkbox" id="demo" name="status" class="status" style="display: none; ">
<span class="CheckBox">
<span class="CBContent">Block</span>
<span class="CBPart"></span>
</span>
</div>

Start code Jquery:

$(document).ready(function(){
$('.CheckBox').on("click", function(){

what should be further actions ?

Upvotes: 2

Views: 70

Answers (2)

Adil
Adil

Reputation: 148140

You need to use jquery prev(),

Live Demo

$(document).ready(function() {
    $(document).on("click", '.CheckBox', function() {
        previousId = $(this).prev().attr('id');
        alert(previousId);
    });
});​

Upvotes: 2

Chandu
Chandu

Reputation: 82913

Try this:

$('.CheckBox').on("click", function(){
  var $checkbox = $(this).prev();
  var $id = $checkbox.attr("id");
});

Upvotes: 3

Related Questions