0

I'm in a situation where I can't use jquery to select an element inside the DOM and trying to figure out how to use Javascript to do it.

The jquery path is $('.link-linkedin a').attr('href');

I'm trying to get to the href and open it in a new tab.

I found this - http://javascript.info/tutorial/searching-elements-dom and tried doing

var elem = document.getElementsByClassName('link-linkedin')
var list = elem.getElementsByTagName('a')

But I'm getting the error:

Uncaught TypeError: elem.getElementsByTagName is not a function(…)

Is there a better way to do this?

5
  • getElementsByClassName() returns an array. you probably want to use elem[0].getElementsByTagName('a') to reach your link. replace 0 by the index your '.link-linkedin' is at Commented Nov 20, 2015 at 16:14
  • 1
    Are you asking how to click an href using javascript or how to properly use getElementsByClassName and getElementsByTagName? Commented Nov 20, 2015 at 16:16
  • ahh! thanks for explaining! Commented Nov 20, 2015 at 16:25
  • Am I the only one confuse? What are you trying to do? If you want to get a dom element into a var, give that element an id and then do document.getElementById("MyLink");. But you also have no semi-colons in this code, which if that is the way you have it in your file, you are of course going to have problems. Commented Nov 20, 2015 at 16:28
  • And do not call a variable with the name "list". That is the sort of word that can be reserved and give you problems, and it tells another programmer nothing of what the variable is used for - list of what? Commented Nov 20, 2015 at 16:29

3 Answers 3

3
// where `linkIndex` , `aIndex` are index of each respective element
// to be selected from collections returned by `.getElementsByClassName` ,
// `.getElementsByTagName`
var linkIndex = 0, aIndex = 1;
var elem = document.getElementsByClassName('link-linkedin')[linkIndex];
var list = elem.getElementsByTagName('a')[aIndex];
var href = list.href;
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need javascript for that. Just add target="_blank" to your anchor element.

Something like this:

<a href="whatever" target="_blank">whatever</a>

This will open the link on a new tab when you click it.

Comments

0

I am going to go ahead and put this as an answer. First, it is unclear what you are trying to do, but some things will help your code.

Put semi-colons at the end of each instruction.

Do not call a variable "list" - list of what - use better naming, and avoid words that could be used as a reserved word.

If you are trying to reference an element in the DOM, give it an ID, and then use document.getElementById to get at it.

<a href="~/Customers.html" id="LinkToCustomers" target="_blank">Customers</a>

And to reference it in your javascript:

var ListOfCustomers = document.getElementById("LinkToCustomers");
window.open(ListOfCustomers.getAttribute("href"));

1 Comment

Ahh, naming conventions are important but I get lazy. :-) Thank you for pointing that out!

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.