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>

  1. to innertext of element need use text() method, not property
  2. 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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -