Search code examples
phpjqueryincrementdecrement

how to get the html value of a brother div


Im sorry for the title but i did not know how to frame a title for this question

Question:

i have a container div and two child divs.the first child div contains a numeric value which i want incremented every time the second child div is clicked

CODE:

 <div class="container1">
    <span class="like_counter">2</like>
    <span class="like_unlike">Like</span><!--if the text within is like then on click it will change the inner text to unlike and increment the value in like counter by 1.if it is unlike it will decrement it by 1 and change inner text to Like-->
 </div>

Note the first child selectors are not an option because it would affect every first child div


Solution

  • Answers, used prev function not very good, if the order of items changes, code stop working.

    Try this:

    $(".like_unlike").bind("click", function(e) {
        e.preventDefault();
        var likeCounter = $(this).siblings(".like_counter");
        likeCounter.html(parseInt(likeCounter.html()) + 1);
    });