I have a function in Javascript
, that is supposed to iterate through a table of links to check if an href
is empty. If the href
is empty, it adds a block of text between the anchors to indicate no link added yet. The code I have is below:
function isWorking(){
//Variable declaration
var anchor, rows, i, link, x, y;
anchor = document.getElementById("myTable");
rows = anchor.getElementsByTagName("TR");
for(i = 0; i < (rows.length - 1); i++){
x = rows[i].getElementsByTagName("A");
link = x.getAttribute("href");
y = link.getElementsByTagName("FONT")[0];
if(link = ""){
y.innerHTML += "<b><font color=red> (not added yet)</font></b>";
}
}
}
Currently, it doesn't really do anything. I'm not very proficient enough in Javascript
so I am having a really hard time understanding what may need to change to make this code work correctly.
Edit: So here is an example of the table I'm using in HTML:
<table id="myTable">
<tr><td><a href="" target="_blank"><font size="4" color="white">C</a></font></td></tr>
<tr><td><a href="" target="_blank"><font size="4" color="white">A</a></font></td></tr>
<tr><td><a href="" target="_blank"><font size="4" color="white">B</a></font></td></tr>
</table>
In the example I include 3 blank links (A, B, and C). Since each links href
is empty, I want to append the text with a the text in my if in the code.
if(link = "")
is invalid.. You are defining the value by the =
operator. It should be if(link == "")
.. Your code can also be minified - don't really know what you're defining the table and rows for. Just to get the a link? A better way:
var links = document.querySelectorAll('#mydiv a'),
// gets all a links inside [mydiv] div...
i, href;
for(i = 0; i < links.length; i++) {
href = links[i].getAttribute('href');
//check the length of href.. is it less than 1 or is a space
if(href.trim().length < 1) {
alert(i + ' is null: ' + href);
}
}