0

Supposing I have the following javascripts inserted in my HTML code...

<a href="http://example.com" data-example="my example">Example #1</a>
<script src="http://example.com/javascript.js"></script>

<a href="http://example.com" data-example="my example">Example #2</a>
<script src="http://example.com/javascript.js"></script>

<a href="http://example.com" data-example="my example">Example #3</a>
<script src="http://example.com/javascript.js"></script>

How do I get the value of data-example in the 3rd link (Example #3)?

4
  • document.querySelectorAll('a')[2].dataset.example is one way in pure JS Commented Jun 5, 2015 at 15:27
  • It's very easy to write the code if you have a static or constant index number of the array.. But what if you don't.. like document.querySelectorAll('a')[n].dataset.example Commented Jun 5, 2015 at 15:47
  • 1
    All of the answers given below are also based on static markup/array index number. That is what your question specifically asks. If it is not static then you need some method other than an index to be able to find what you are looking for, the same with all the answers below. So what is it you really want? Commented Jun 5, 2015 at 16:04
  • Yes you should specify in your question if the content is static or dynamic. Provide static content, and you will get a static answer. :) Commented Jun 5, 2015 at 17:03

6 Answers 6

3

use eq(). Reduce the set of matched elements to the one at the specified index.

$('a[data-example]').eq(2).text();

Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

The way I read, the OP wants the value of the data-example attribute. I could be wrong.
1

Try this:

var data = $('a:eq(2)').data('example');

See :eq

Comments

1

You could also use psuedo selectors. DEMO

$('a:nth-of-type(3)').data('example')

Comments

0

I suggest you not to rely on "getting all the anchors that have data-example on it", but doing something more explicitly, like specifying a class for these links:

<a class="my-link" href="http://example.com" data-example="my example">Example #1</a>
<script src="http://example.com/javascript.js"></script>

<a class="my-link" href="http://example.com" data-example="my example">Example #2</a>
<script src="http://example.com/javascript.js"></script>

<a class="my-link" href="http://example.com" data-example="my example">Example #3</a>
<script src="http://example.com/javascript.js"></script>

And then, using jQuery

 $(".my-link:eq(3)").data("example")

Why the suggestion? because you might later have other links with data-example too

Comments

0

If you want to show the value of data-example attribute, you can use :before or :after pseudo class in CSS.

a:before {
    content: attr(data-example);
}

Working Fiddle

Comments

0

Here a generally usable function for this purpose.

function getEqData(selector, data, num){
    return $(selector).eq(num-1).data(data);
}

var data = getEqData('a', 'example', 3);
console.log(data);

JSFiddle

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.