Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why is it that when i use jquery to change the href of an anchor tag, it just puts the link on the end of the current url?

I have an app that makes an API call to return the Wikipedia link for a selected country. When you select the country, the app uses jQuery to change the href of the link to the relative country:

     <a id="txtWiki" class="text-info" target="_blank"></a>


      $('#txtWiki').attr('href', (result['data'][0]['wikipediaUrl']));

The link does change, however when I click on the link it is adding the link to the end of the current URL in the bar – for example if I wanted it to go to https://en.wikipedia.org/wiki/France, clicking the link will take me to localhost/myappname/en.wikipedia.org/wiki/France.

I am unsure why this is happening, I thought that adding the _blank target attribute would solve this but it is not. What am I missing here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I believe your code isn’t functioning as intended due to the use of a relative URL instead of an absolute one. Please try the following modification:

$('#txtWiki').attr('href', 'https://' + result['data'][0]['wikipediaUrl']);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading