0

Can you tell me, how to get data from a link attribute?

Example :

<a href="#" target="2">link</a>

I want get the data from 'target' attribute to send to server.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Possible duplicate of [jquery: get value of custom attribute](https://stackoverflow.com/questions/7177512/jquery-get-value-of-custom-attribute) – Gagantous May 09 '19 at 04:20

1 Answers1

0

jQuery

You can also do the same thing in jQuery using the .attr() function.

var target = $("#link").attr("target");

console.log(target);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" target="2" id="link">link</a>

Pure Javascript

You can use .getAttribute() like below. (I used document.getElementById() to get the element, and .getAttribute to get the value of the target attribute)

var target = document.getElementById("link").getAttribute("target");

console.log(target);
<a href="#" target="2" id="link">link</a>
Aniket G
  • 3,471
  • 1
  • 13
  • 39
  • Can you show me the jquery code? I want use jquery code, not a pure JavaScript –  May 09 '19 at 04:19