Cleaven
Cleaven

Reputation: 974

Javascript not working onclick

I have this javascript function here, and I'm pretty new to javascript, could someone please tell em why its not working?

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="EduAssist.WebForm1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="assets/scripts.js"></script>
<script type="text/javascript">
    $("#clickme").click(function (e) {
        var selected = $("#checkboxes input:checked").map(function (i, el) { return el.name; }).get();
        alert("selected = [" + selected + "]\nas string = \"" + selected.join(";") + "\"");
    });
</script>
<div id="checkboxes">
    <input id="chkbx_0" type="checkbox" name="c_n_0" checked="checked" />Option 1
    <input id="chkbx_1" type="checkbox" name="c_n_1" />Option 2
    <input id="chkbx_2" type="checkbox" name="c_n_2" />Option 3
    <input id="chkbx_3" type="checkbox" name="c_n_3" checked="checked" />Option 4
</div>
<input type="button" id="clickme" value="click me, now!" onclick="clickme" />

I'm working in c# and asp.net

Thanx

Upvotes: 1

Views: 66

Answers (1)

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

You need to move your code in jquery document ready block

$(document).ready(function(){
    $("#clickme").click(function (e) {
        var selected = $("#checkboxes input:checked").map(function (i, el) { return el.name; }).get();
        alert("selected = [" + selected + "]\nas string = \"" + selected.join(";") + "\"");
    });
});

Also, remove the onclick attribute from the html. It is not required as you are binding the event using jquery.

Upvotes: 3

Related Questions