How To Access This Attribute Using Jquery, Given A Div Defined By This
I have a div which is defined via a this. Imagine it is the var div below:
div 1
div 2
Solution 1:
Try
var itemidValue = $('.action').parent('td').attr('itemid');
Proof of concept: http://jsfiddle.net/AwM56/
Solution 2:
If you're going to use custom attributes, you should really be doing it with HTML5 data-*
attributes, so use data-itemid='desired number'
instead, then use the .data()
rather than .attr()
function to get the value.
HTML:
<tddata-itemid='desired number'><div>div 1</div><divclass="action">div 2</div></td>
jQuery:
var div = $('.action');
var itemId = div.parent().data('itemid');
Solution 3:
Instead of using .parent()
, you can try using parents() and specify what you are looking for.
Example:
$(".action").parents("td") // will return the TD element
Proof: http://jsfiddle.net/9LQB3/
I use the parents() method instead of parent() as the parents() looks higher up the hierarchy, and not just the immediate parent.
Solution 4:
.parent()
should be OK.
Try this:
<html><head><scripttype="text/javascript"src="jquery.min.js"></script><scripttype="text/javascript">
$(function() {
var div = $('.action');
console.log('HEP: ' + $(div).parent().attr('itemid'));
});
</script></head><body><table><tr><tditemid='desired number'><div>div 1</div><divclass="action">div 2</div></td></tr></table></body></html>
Solution 5:
Have you tried using div.closest('td')
?
Post a Comment for "How To Access This Attribute Using Jquery, Given A Div Defined By This"