I have an jsp page with html and js like below. Now I want to keep the js function test() to another js file. I m trying to write that using jquery.
<div class="class1">
<a href="javascript:void(0)" onclick="test();" id="add_another" ><b>Add another user</b></a>
</div>
JS function
function test(){
alert("I am here");
//other code
}
I tried to write like this
$(function() {
$('#add_another').click(test);
});
But its not working. I am not sure if its because its having href="javascript:void(0)" When the user clicks Add Another User link, it show display another div. That is the test() doing. But the alert itself is not ing. Can somebody please help me in this?
I have an jsp page with html and js like below. Now I want to keep the js function test() to another js file. I m trying to write that using jquery.
<div class="class1">
<a href="javascript:void(0)" onclick="test();" id="add_another" ><b>Add another user</b></a>
</div>
JS function
function test(){
alert("I am here");
//other code
}
I tried to write like this
$(function() {
$('#add_another').click(test);
});
But its not working. I am not sure if its because its having href="javascript:void(0)" When the user clicks Add Another User link, it show display another div. That is the test() doing. But the alert itself is not ing. Can somebody please help me in this?
Share Improve this question asked Nov 25, 2013 at 9:59 user1049057user1049057 4594 gold badges15 silver badges37 bronze badges 2-
Have you tried
javascript:;
instead ofjavascript:void(0)
? – fnkr Commented Nov 25, 2013 at 10:01 - 1 It's working for me. Please check jsfiddle/Ub6Fk – Ishan Jain Commented Nov 25, 2013 at 10:04
1 Answer
Reset to default 4To replicate javascript:void(0)
you can use event.preventDefault() in js like this.
$(function () {
$('#add_another').click(function (e) {
e.preventDefault();
test();
});
});