Wow. So many things wrong in such a small amount of code. Reformatting what you posted so it's readable:
<title>Display the Image
<center>
<table width="50%" border="1" cellpadding="5" cellspacing="5">
<thead>
<tr>
<th>ID NO</th>
<th>USER NAME</th>
<th>IMAGE</th>
</tr>
</thead>
// Function to handle image removal
function removeImage()
{
var as=document.getElementById('$image')
}
as.remove();
<tbody>
<tr>
<td> </td>
<td> </td>
<td>" width="100" height="100" alt="image">Remove Image</td>
</tr>
</tbody>
</table>
</center>
First, that's not HTML. That's a badly formatted fragment of HTML. It's also not even correct as there is no
html
or
body
tags. The
title
tag is only valid inside a
head
tag, which you also do not have.
You have what I assume is a partially missing
img
tag, with width, height, and alt attributes that will be treated like text instead of HTML.
You have javascript code in the middle of a table, but not wrapped in a
script
tag. That's not going to work at all.
Your
getElementById
call isn't going to return anything because putting a $ character at the beginning of an ID doesn't work for ID values. YOu have to preface the ID with a # character to select by a tag ID. Further, you don't have any tags that specify any IDs at all!
To select a tag by an attribute value (id, alt, width, height are all attributes in HTML), you have to specify the attribute and value you're looking for inside square brackets in your selector specification:
var tags = document.querySelector('img[alt="image"]');
The call to
querySelector
will return a collection of ALL
img
tags that have an
alt
attribute, not just one tag.
Your call to
getElementById
will return the first tag it finds, not a collection. You have to know about the differences between a single object and a collection and handle what those functions return differently.
[EDIT - Things I forgot to type]
Your
as
variable goes out of scope, meaning it doesn't exist anymore, when execution leaves the curly braces where you defined
as
. Your call to
remove
will fail because
as
variable no longer exists.
And finally, where do you even call the
removeImage
function from? You've got nothing making that call, so nothing is going to happen.
I'm not fixing this for you because it's homework. If I do it for you, you learn nothing but dependence on others to fix your own mistakes.