0

my html is like this:

<button onclick="prints(this.nextElementSibling.attributes)">me!</button>
<a href="project/webdev/convert.js"> convert! </a>

the prints function just outputs the function parameters onto console log. I need to find the href value of the a tag immediately beside the anchor tag. i.e on clicking the button, I need to get the value "project/webdev/convert.js"

The "this.nextElementSibling.href" gives me "http://127.0.0.1:8080/project/webdev/convert.js". Is there anyway I can instead get the relative address? i.e project/webdev/convert.js.

Anyhelp would be very much appreciated.(I cannot use Jquery, I want to do this with vanilla javascript)

Vamshi
  • 64
  • 1
  • 9
  • Well you can always parse the string to get the relative path https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substring – Huangism Jun 08 '17 at 13:48
  • here's link to a generalized solution: https://stackoverflow.com/questions/5494691/cutting-a-string-at-nth-occurrence-of-a-character – Raj Jun 08 '17 at 13:57
  • Thank you very much for those links. I knew I could have done something similar to that, but i was looking for a way to just get the href value directly. – Vamshi Jun 09 '17 at 04:41

1 Answers1

1

You could try this:

const btn = document.querySelector('button');
const link = btn.nextElementSibling.getAttribute('href');

btn.addEventListener('click', () => console.log(link));
<button>me!</button>
<a href="project/webdev/convert.js"> convert! </a>
VilleKoo
  • 2,827
  • 2
  • 14
  • 23