Mythily
Mythily

Reputation: 57

anchor tag onclick is not working

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

Upvotes: 1

Views: 9610

Answers (3)

Kay
Kay

Reputation: 498

EnterComments() is called twice here.

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

It should work then

Upvotes: 1

chandru
chandru

Reputation: 17

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>

Upvotes: 0

Pranav
Pranav

Reputation: 666

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

Upvotes: 2

Related Questions