最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to get parent div attribute values in jquery? - Stack Overflow

programmeradmin3浏览0评论

I need data-offerId of parent div but it's not working properly. Please suggest me.

HTML

<div class="rows" data-offerid="123487 ">15<span class="mth">.– /mth</span><span class="clear-row"></span></div>
<div class="rows" data-offerid="123486 ">15<span class="mth">.– /mth</span><span class="clear-row"></span></div>

Jquery:

 $('.clear-row').click(function(){
     var offers = $('.clear-row').parent().attr('data-offerid');
     console.log(offers);
 });

I need data-offerId of parent div but it's not working properly. Please suggest me.

HTML

<div class="rows" data-offerid="123487 ">15<span class="mth">.– /mth</span><span class="clear-row"></span></div>
<div class="rows" data-offerid="123486 ">15<span class="mth">.– /mth</span><span class="clear-row"></span></div>

Jquery:

 $('.clear-row').click(function(){
     var offers = $('.clear-row').parent().attr('data-offerid');
     console.log(offers);
 });
Share Improve this question asked Sep 1, 2017 at 8:25 jinfyjinfy 1571 gold badge1 silver badge12 bronze badges 3
  • Possible duplicate of How to get the parent element value using jquery? – always-a-learner Commented Sep 1, 2017 at 8:26
  • try .parent().data('offerid'); – Melcma Commented Sep 1, 2017 at 8:27
  • Sometimes $(".clear-row").closest("div") works better than parent() – kevinSpaceyIsKeyserSöze Commented Sep 1, 2017 at 8:33
Add a ment  | 

4 Answers 4

Reset to default 6

Inside the handler, you should be using this not repeating the selector .clear-row

$('.clear-row').click(function(){
   var offers = $(this).parent().attr('data-offerid');
   console.log(offers);
});

You can also use data() instead of attr()

var offers = $(this).parent().data('offerid');

Here you go with one more way using jQuery closest method https://jsfiddle/464g5hzq/

$('.clear-row').click(function(){
  var offers = $(this).closest('div.rows').data('offerid');
  console.log(offers);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rows" data-offerid="123487 ">15<span class="mth">.– /mth</span><span class="clear-row"></span></div>
<div class="rows" data-offerid="123486 ">15<span class="mth">.– /mth</span><span class="clear-row"></span></div>

Instead of using .attr to get the data attribute, please use .data(attribute-name); if you have data attribute like data-offerid.

Hope this will be useful to you.

Use this inside your click event, to target the cleararrow element your clicked on

$(this).parent().attr('data-offerid')

$('.clear-row').click(function() {
  var offers = $(this).parent().attr('data-offerid');
  console.log(offers);
});

Working example

$('.clear-row').click(function() {
  var offers = $(this).parent().attr('data-offerid');
  console.log(offers);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rows" data-offerid="123487 ">15<span class="mth">.– /mth</span><span class="clear-row">clear</span></div>
<div class="rows" data-offerid="123486 ">15<span class="mth">.– /mth</span><span class="clear-row">clear</span></div>

Try this way:

$('.clear-row').click(function(){
    var offers = $(this).parent().attr('data-offerid');
    console.log(offers);
});

//OR

var offers = this.parentNode.getAttribute("data-offerid");   // plain javascript

Helper Link to understand: parent

发布评论

评论列表(0)

  1. 暂无评论