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>
1
  • 2
    a great place to start would be the api documentation -> api.jquery.com Commented Jan 17, 2013 at 11:30

2 Answers 2

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') );
Sign up to request clarification or add additional context in comments.

2 Comments

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..
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"));
2

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.