Reputation: 83
how can I get only the link?
<a id="skip" href="http://google.de">
This doenst work:
var doc = document.getElementById('skip');
var array = doc ? doc.getElementsByTagName('a') : [
];
if (array.length > 2)
array[0].href = array[1].href;
alert(array[0]);
Upvotes: 0
Views: 41
Reputation: 79032
Your variable doc
will never be an array as document.getElementById
will only return the matched element, or undefined.
This code is all you need:
var doc = document.getElementById('skip');
alert(doc.href);
Extra:
This conditional statement would return the element, and never be false:
var array = doc ? doc.getElementsByTagName('a') : [];
This would always return false as the html element does not have a .length
property:
if(array.length > 2)
This will cause an error as array
is not an array.
alert(array[0]);
Upvotes: 2