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 thanparent()
– kevinSpaceyIsKeyserSöze Commented Sep 1, 2017 at 8:33
4 Answers
Reset to default 6Inside 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