javascript - anchor tag onclick is not working - Stack Overflow

HTML code: <a href="javascript:void(0);" id="btnSaveComments" runat="serv

HTML code:

<a href="javascript:void(0);" id="btnSaveComments" runat="server"
        onclick="EnterComments();" class="btn btn-sm btn-info no-radius">&nbsp;Send</a>

JavaScript code:

$(document).ready(function() {
    $("#btnSaveComments").click(function(e) {
        EnterComments();
        e.preventDefault();
    });
});

function EnterComments() {
    alert("test");
}

When I checked with debugger, it got inside document.ready but it didn't get inside the function EnterComments

HTML code:

<a href="javascript:void(0);" id="btnSaveComments" runat="server"
        onclick="EnterComments();" class="btn btn-sm btn-info no-radius">&nbsp;Send</a>

JavaScript code:

$(document).ready(function() {
    $("#btnSaveComments").click(function(e) {
        EnterComments();
        e.preventDefault();
    });
});

function EnterComments() {
    alert("test");
}

When I checked with debugger, it got inside document.ready but it didn't get inside the function EnterComments

Share Improve this question edited Jul 16, 2014 at 5:41 Regent 5,1763 gold badges23 silver badges35 bronze badges asked Jul 16, 2014 at 5:24 MythilyMythily 571 gold badge4 silver badges10 bronze badges 8
  • put e.preventDefault(); before EnterComments. – Surinder ツ Commented Jul 16, 2014 at 5:27
  • Its working for me jsfiddle/deepakmanwal/8nSSL – Manwal Commented Jul 16, 2014 at 5:27
  • it works fine - jsfiddle/43fC3 – Sudharsan S Commented Jul 16, 2014 at 5:27
  • did you include jquery.min.js?? – Pranav Commented Jul 16, 2014 at 5:28
  • Please make sure your jquery library path – Sudharsan S Commented Jul 16, 2014 at 5:28
 |  Show 3 more ments

3 Answers 3

Reset to default 2

please see the code below :

HTML :

 <span class="input-group-btn"><a href="javascript:void(0);" id="btnSaveComments" runat="server"
                 onclick="EnterComments();" class="btn btn-sm btn-info no-radius"><i class="icon-share"></i>&nbsp;Send</a>
                </span>

JS :

function EnterComments() {
    console.log("test");
    alert("hi");
        }

Hope you find it useful.

Demo : http://plnkr.co/edit/Vq0Yi5mu9y0RRK5tIhYt?p=preview

EnterComments() is called twice here.

Remove onclick="EnterComments();" and try once.

It should work then

Try this out:

<a href="#" id="anchor_tag" onclick="alert('You are clicked TEST TEXT');">TEST TEXT</a>

SCRIPT CODE:

<script type="text/javascript">
$(document).ready(function(){
    $('#anchor_tag').click(function() {
        alert('You have clicked test text'); 
    });


    enter code here

}); 
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745163665a4614519.html

相关推荐

  • javascript - anchor tag onclick is not working - Stack Overflow

    HTML code: <a href="javascript:void(0);" id="btnSaveComments" runat="serv

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信