I have multiple tables wher each row is defined by an ID and a class. The words in each row are actually href links. I have one button with id testbuttonba. If testbuttonba is clicked, I want the following to occur:
1) href for ID table1 to be disable.
2) href for ID table2 and table3 to still be enabled.
My code below does not work (all links are still enabled after clicking):
HTML
<body>
<button class="btnba" id="testbtnba" onclick="function2()">BA</button>
/* 1st Table */
<table>
<tr>
<th><font size="1">Capability Group</font></th>
</tr>
<tbody id="table1">
<tr>
<td><a href="showdoc.html"><font size="1"><strong>A. Organisational Content</strong></font></a></td>
</tr>
</table>
/* 2nd Table */
<table>
<tr>
<th><font size="1">Capability Group</font></th>
</tr>
<tbody id="table2">
<tr>
<td><a href="showpdf.html"><font size="1"><strong>B. Basic Requirements</strong></font></a></td>
</tr>
</table>
/* 3rd Table */
<table>
<tr>
<th><font size="1">Capability Group</font></th>
</tr>
<tbody id="table3">
<tr>
<td><a href="showexcel.html"><font size="1"><strong>C. Rules and Regulations</strong></font></a></td>
</tr>
</table>
JavaScript
<script>
/*diasble the first link - not working*/
function function2() {
document.getElementById("table1").href = "#";
}
return false;
</script>
You shouldn't remove the
hrefon the<a/>(at least without storing in some way). You can create a toggle that will determine whether or notEvent.preventDefault()will be called when the button is clicked.This will do the trick:
The reason it does not work in your snippet above is only because your selector doesn't select the
<a/>element, doing this will rectify that:Keep in mind, that there is no way to re-enable the link using the code in this way :o
Update: Replaced the
<font/>tags with some CSS. The<font/>element is obsolete. Hope that helps,