javascript - How to alert the value using $this in jquery? -
i have code , want alert value of "first div" when click button on first object , "second div" when click button on 2nd object. .
how ? :)
$(document).ready(function(e) { $('.clickme').click(function() { var selected = $(this).closest('.rel-wrap').find('.my-div').text; alert(selected); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div class="main-wrapper"> <div class="rel-wrap"> <input type="button" class="clickme" value="click me"> <div class="my-div"> first div </div> </div> <div class="rel-wrap"> <input type="button" class="clickme" value="click me"> <div class="my-div"> second div </div> </div>
- to innertext of element need use
text()
method, not property - you can use
next()
, no need of traversing parent interested element
demo
$(document).ready(function(e) { $('.clickme').click(function() { var selected = $(this).next('.my-div').text(); alert(selected); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div class="main-wrapper"> <div class="rel-wrap"> <input type="button" class="clickme" value="click me"> <div class="my-div"> first div </div> </div> <div class="rel-wrap"> <input type="button" class="clickme" value="click me"> <div class="my-div"> second div </div> </div> </div>
Comments
Post a Comment