Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get a textbox in a table

Tags:

jquery

I would like to select a textbox being dynamically created by an asp.net server control (listview) For each row in the table I need to run some javascript functionality over it.

Below I am trying to select the textbox based off of the name haveing the text. Is there a "better" way of doing this?

      $('#tblNewAttendees tr').each(function() {
   var test = $(this).find('input[name$="txtFirstName"]').val();
      });
    }

Html code

<table>
     <thead>
     </thead>
     <tbody>
           <tr>
                 <td>
                   <asp:TextBox ID="txtFirstName" Text='<%#Eval("FirstName")%>'      
                runat="server" Width="80px" />
                 </td>
          </tr>
     </tobdy>
</table>
like image 753
gh9 Avatar asked May 09 '26 08:05

gh9


1 Answers

Assign a specific CSS class to all those text boxes, and then use class selectors to select those.

Suppose you have assigned myClass as CSS class value. Then use the following to select them -

$('#tblNewAttendees tr').each(function() 
{
    var test = $(this).find('input.myClass').val();
});

The reason is, as far as I remember, the names that you assign to a text box is replaced by ASP.NET with something new after it parses the document. Suppose you have a text box whose name is myBox. Then after the page is parsed, its name may become something like ct_1001_myBox_123 or something. So it's better to use a CSS class to select them in the client side.

like image 164
MD Sayem Ahmed Avatar answered May 10 '26 23:05

MD Sayem Ahmed