I have this code:
var object1 = getElementByID('obj1');
alert(object1.getAttribute('href'));
This displays correctly the URL in the href attribute of the object. But, when I try:
var object1 = getElementByID('obj1');
object1.setAttribute('href','someotherURL');
alert(object1.getAttribute('href'));
This fails. The code doesn't work on FF so I can only test it in IE, no Firebug =/. I have also tried.
object1.href = "someotherURL";
but it also fails. Does anyone knows why I cant modify the attribute? let me know if I need to provide more information.
Regards.
UPDATE: HTML:
<table class="msrs-topBreadcrumb" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td>
<span>
<div>
<a href="SomeURL">A Name</a> >
<a href="AnotherURL">A Name2</a> >
<a href="Third URL">A Name3</a>
</div>
</span>
</td>
<td align="right">
<span>
<a href="URL1">A Name</a> |
<a href="URL2">A Name2</a> |
<a href="URL3">A Name3</a> |
<a href="URL4">A Name4</a>
</span>
</td>
</tr>
</table>
FUNCTION:
function mySubscriptions()
{
var mySubsObj = getElementsByClass('msrs-topBreadcrumb')[0].firstChild.childNodes[0].childNodes[1].childNodes[0].childNodes[2];
alert(mySubsObj.getAttribute("href"));
}
I have this code:
var object1 = getElementByID('obj1');
alert(object1.getAttribute('href'));
This displays correctly the URL in the href attribute of the object. But, when I try:
var object1 = getElementByID('obj1');
object1.setAttribute('href','someotherURL');
alert(object1.getAttribute('href'));
This fails. The code doesn't work on FF so I can only test it in IE, no Firebug =/. I have also tried.
object1.href = "someotherURL";
but it also fails. Does anyone knows why I cant modify the attribute? let me know if I need to provide more information.
Regards.
UPDATE: HTML:
<table class="msrs-topBreadcrumb" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td>
<span>
<div>
<a href="SomeURL">A Name</a> >
<a href="AnotherURL">A Name2</a> >
<a href="Third URL">A Name3</a>
</div>
</span>
</td>
<td align="right">
<span>
<a href="URL1">A Name</a> |
<a href="URL2">A Name2</a> |
<a href="URL3">A Name3</a> |
<a href="URL4">A Name4</a>
</span>
</td>
</tr>
</table>
FUNCTION:
function mySubscriptions()
{
var mySubsObj = getElementsByClass('msrs-topBreadcrumb')[0].firstChild.childNodes[0].childNodes[1].childNodes[0].childNodes[2];
alert(mySubsObj.getAttribute("href"));
}
Share
Improve this question
edited Nov 13, 2013 at 17:46
varrtto
asked Nov 13, 2013 at 17:34
varrttovarrtto
1012 gold badges3 silver badges11 bronze badges
5
- It'd help to know what HTML element is involved! – Pointy Commented Nov 13, 2013 at 17:37
-
3
It's
getElementById
notgetElementByID
. – j08691 Commented Nov 13, 2013 at 17:37 -
Also you don't need
setAttribute()
andgetAttribute()
to access or modify properties like "href". – Pointy Commented Nov 13, 2013 at 17:40 - I'm sorry, this is just a sample of what the code actually does. I'll provide a more plete code. – varrtto Commented Nov 13, 2013 at 17:41
-
1
so I can only test it in IE, no Firebug =/.
IE has a debugger! Are you using IE7 which is the last version of a browser without the console? lol – epascarello Commented Nov 13, 2013 at 17:54
1 Answer
Reset to default 4Use document.getElementById
and it works fine:
var object1 = document.getElementById('obj1');
object1.setAttribute('href','someotherURL');
alert(object1.getAttribute('href'));
Tested in Firefox, IE and Chrome.
Demo: http://jsfiddle/Guffa/zdrP9/