2

This is the code I have written with angular js. using jquery I want to get the "data-index" attribute value

<div class="claimed" droppable value="2">

        <div class="phase1" ng-repeat="item in Phase1claimed"
            data-index="{{$index}}" draggable value="2">
            <div class="ClaimedAccordion" accordion-title="{{item.name}}" unclaimhi="unclaimhi('1');">

                {{item.newLabel}} <br /> {{item.desc}} <br /> {{item.effort}} <br />
                {{item.owner}} <br /> {{item.issues}} <br /> {{item.comments}} <br />
                {{item.dependency}} <br /> {{item.content}}
            </div><br/>
        </div>
    </div>
user1966260
  • 117
  • 1
  • 2
  • 5

2 Answers2

7

You can use data() to get the data attributes data-index using the class selector.

Live Demo

$('.phase1').data('index');

You can use descendant selector to find out element with phase class within element with claimed class.

$('.claimed .phase1').data('index');

You can use attr to get the attribute but using the attr will not get you value being changed by javascript. Its worth reading this post for difference between data and attr.

Live Demo

<a id="foo" data-foo="bar" href="#">foo!</a>

alert( $('#foo').attr('data-foo') );
alert( $('#foo').data('foo') );
Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
  • how can i get the index number of corresponding data like i have three data-foo as bar for different element and i want to get the index of the element using their data attribute.. – Ankit Sharma Apr 23 '13 at 19:35
  • As for as my understanding you want to get the element by attributes, $('[data-foo]').each(function(){ alert(this.tagName); }); you can use eq to get particular element alert($('[data-foo]').eq(0).attr("id")); – Adil Apr 24 '13 at 10:09
2

You can also use attr() jQuery API function to get the data attribute

$('.phase1').attr('data-index');
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nrsharma
  • 2,532
  • 3
  • 20
  • 36