最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript:void(0) in jquery - Stack Overflow

programmeradmin11浏览0评论

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 of javascript: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
Add a ment  | 

1 Answer 1

Reset to default 4

To replicate javascript:void(0) you can use event.preventDefault() in js like this.

$(function () {
    $('#add_another').click(function (e) {
        e.preventDefault();
        test();
    });
});
发布评论

评论列表(0)

  1. 暂无评论