0

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.

3
  • Do you get any errors in the browser's console? Are you using an old enough version of jQuery that .live() is still supported? Commented Apr 13, 2017 at 3:13
  • Try placing your <script></script> before the closing </body> tag. Also make sure you're calling jquery before your <script> Commented Apr 13, 2017 at 3:16
  • Hi, thank you all for you answers but unfortunately nothing worked Commented Apr 13, 2017 at 3:29

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

are you using table with id="tblGrid" any where outside partial view? If not then move your script tag in partial view
no, only in partial view...and i have also tried doing that but no luck..
check the errors using firebug but no errors in the console
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.