Placing Click event on ador tag in html from javascript?
Can anyone help, there seems to be an issue with the onclick event of the anchored tag, it works on the image. I have it
this.whereAreWe = document.getElementById('where_are_we');
this.whereAreWe.onclick = this.whereAreWe;
I have placed the A tag using the id "where_are_we" ...
but it never gets executed. If I change it to an image it works.
I also put href = "#"
Is there something special about binding tags and applying onclick through code?
I also tried to remove the href, if I remove the href it doesn't show me the little hand icon.
I put a breakpoint in the function and with the image it injects, but using an anchor, it doesn't
Any ideas?
source to share
I also tried removing the href.If I remove the href it doesn't show me the little hand icon.
To specify the URL of the target document or web resource, you need the "href" attribute for the "a" tag. If you haven't specified it - mouseover won't change the cursor. You can of course use CSS to change this, but that's a different matter.
source to share
The code you provided is confusing. The following code works correctly for me:
<a href="#" id="whereWeAre">a link</a>
<script type="text/javascript">
var whereWeAre = document.getElementById("whereWeAre");
function testClick() {
alert("You clicked!");
}
whereWeAre.onclick = testClick;
</script>
If your example was a little more specific, we might be more helpful.
source to share
These are 2 problems with your javascript. Using "this" and binding the onclick event to the link of the DOM element for the HREF. Try this instead:
var whereAreWe = document.getElementById("where_are_we");
whereAreWe.onclick = function(){
alert("Click event on Where are We");
return false;
};
source to share