I was generating a table dynamically. Inside the td data , there is a hyperlink.inturn it should call a function. why the below code snippet was not working. Nothing happening when i click on the link.
<a href='#' onclick='function(){alert('hiii');}'>
I was generating a table dynamically. Inside the td data , there is a hyperlink.inturn it should call a function. why the below code snippet was not working. Nothing happening when i click on the link.
<a href='#' onclick='function(){alert('hiii');}'>
Share
Improve this question
edited Dec 22, 2013 at 5:39
markasoftware
12.7k8 gold badges44 silver badges70 bronze badges
asked Dec 22, 2013 at 5:30
AfjuAfju
571 gold badge3 silver badges10 bronze badges
1
- my code snippet is like : var td ="< a href='#' onclick='function(){alert('hiii');}' >"; – Afju Commented Dec 22, 2013 at 5:36
9 Answers
Reset to default 6It's because of conflicting '
and a funciton definition instead of call (thanks @user1389596):
<a href='#' onclick="alert('hiii')">
that should work. The extra '
s inside of the onclick made the browser view it as the end of the onclick attribute, which isn't what you want. This way, using two different types of quotes, it works fine.
Or, even better, don't use inline handlers:
<a href='#' id="thethingy">
<!--in a galaxy far, far away:-->
<script type="text/javascript">
document.addEventListener('DOMContentLoaded',function(){
document.getElementById('thethingy').addEventListener('click',function(){
alert('hiii');
},false);
},false);
</script>
Event handlers added via innerHTML
will not trigger in some cases for security reasons. It's also incorrect to include function ()
since all this onclick
would do is define an anonymous function and do nothing else. You also have conflicting quotes.
If you are using jQuery, event delegation would make this simpler. Exclude onclick
entirely and use something like:
$(document).on("click", "a", function () { alert("hiii"); });
document
and a
selectors should be as specific as possible.
You can also bind events to elements added to the DOM as needed.
1st Issue
onclick='function(){alert('hiii');}'
This seriously is wrongly coded as per the use of quotes. The proper usage should be any of the following
Proper use of quotes
onclick='function(){alert("hiii");}'
or
onclick="function(){alert('hiii');}"
This much only about the syntax. But there is still something to be done to actually call the function at runtime. Without proper invocation the function will not be executed. That is the 2nd issue.
2nd Issue
Now there are 2 syntax that you may follow to actually execute the function at runtime.
1st syntax:
(function(){...})();
So the code should be-
onclick="(function(){alert('hiii');})();"
Fiddle Demo1
2nd syntax
new function(){...};
So the code should be-
onclick="new function(){alert('hiii');};"
Fiddle demo2
You are defining a function... not calling it.
try this:
<a href="javascript:alert('hi');">
You should not use onclick="function(){alert('hiii');}"
. You may try this code:
<a href="#" onclick="alert('hiii');">
<a onclick='function(){alert('hiii');}' href='#' >
try this one it will work without any problem, no need for event listeners
Although this a quotes problem. Following will solve your problem.
<a href='#' onclick="alert('hiii')">
In case you are using Vue.js like me onclick will not work. You can use this.
<a href="javascript:void(0);" @click="alert('hiii')">
Maybe this can be of help to someone, At first I thought it was due to the fact that href was present, and inserted before the onclick event.
However here is a sample of an a tag that calls a function and passing a parameter.
Both Samples has the onclick event and href properties in different orders.
function helloWorld(message){
alert('Hello world! : ' + message);
};
<a href='#' onclick="helloWorld('href before onclick')">Click Me (href then onclick)</a>
<br>
<br>
<a onclick="helloWorld('onclick before href')" href="#">Click Me (onclick then href)</a>
};