Javascript – td onclick not working

html-tablejavascriptjquery

I am working in MVC4 and using tables, When I set onclick on <td> element of table then related function is not working.

This is how I am doing-

 <tr>

      <td style="cursor:pointer" onclick="D();">
       #= DisplayName #</td>
      </tr>

jQuery function-

<script type="text/javascript">

                $(document).ready(function D() {
                    $('td').click(function (event) { loadPartially(event, '@Url.Contact_PartialView_Main()?ContactID=@Model.ContactID') });
                    alert("aa");
                });
            </script>

When this didn't work I tried giving it an ID-

 <tr>

          <td style="cursor:pointer" id="Open">
           #= DisplayName #</td>
          </tr>

jQuery function for this-

<script type="text/javascript">

                    $(document).ready(function {
                        $('#Open').click(function (event) { loadPartially(event, '@Url.Contact_PartialView_Main()?ContactID=@Model.ContactID') });
                        alert("aa");
                    });
                </script>

I am trying to get load partial event on click of this element of table.

But somehow it is not working. I am not sure if i can use onclick function on <td> element of table.

Best Answer

Change:

$(document).ready(function {

to

$(document).ready(function () {