Adarsh Sharma
Adarsh Sharma

Reputation: 19

javascript/jquery not working in the partial view.

I have a main view and in it I have a partial view. In my partial view, I have a table which shows some results of a search carried out. what I want is that when I click on the row, it should alert the data in that row. but this is not happening.

I tried nearly everything on the internet but couldn't find anything.

Partial view:

@model List
<string>
<table class="table table-striped" id="tblGrid">
   <thead id="tblHead">
      <tr>
         <th>Name</th>
         <th>Email</th>
      </tr>
   </thead>
   <tbody>
      @for (int item = 0; item < Model.Count; item = item + 2)
      {
      <tr>
         <td style="width: 300px">
            @Model[item].ToString()
         </td>
         @{ int temp = item;
         item = item + 1;
         }
         <td style="width: 100px">
            @Model[item].ToString()
         </td>
         @{
         item = temp;
         }
      </tr>
      }
   </tbody>
</table>

Data is populated just fine, but the javascript is not called.

main:

<head>
   <script type="text/javascript">
      $(document).ready(function () {
      $("#tblGrid tr").live(function () {
        $(this).addClass('selected').siblings().removeClass('selected');
        var value = $(this).find('td:first').html();
        alert(value);
      });            
      });
   </script>
</head>
<body>
   <input id="searchAttr" name="searchAttr" />
   <button href="#myModal" id="openBtn" data-toggle="modal" class="btn btn-default">Search</button>
   <div id="searchresults">
   </div>
</body>

Any help provided will be appreciated.

Upvotes: 0

Views: 364

Answers (2)

bearwithbeard
bearwithbeard

Reputation: 331

Are you trying to use jQuery without connecting your view with it?

Add to the top of of your main this

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

Upvotes: 0

Ahefaz
Ahefaz

Reputation: 84

place your script tag in body instead of head and then try

Upvotes: 1

Related Questions