Sunday, April 17, 2011

Jquery Hidden Field in Table

Hi all,

I was wondering if anyone knew of a way to access a hidden field (by client id) within a table row using jquery.

$("#tblOne").find("tr").click(function() {
            var worker = $(this).find(":input").val();
        });

I find that the above works for a row that has only one input, but i need some help figuring out a way to get the value by the inputs name.

Here's the example of a table row. How would i access the two fields by their id's?

<table id="tblOne">
<tr>
<td>
    <asp:HiddenField id="hdnfld_Id" Text='<% Eval("ID") %>'></asp:HiddenField>
</td>
<td>
    <asp:HiddenField id="hdnfld_Id2" Text='<% Eval("ID2") %>'></asp:HiddenField>
</td>
</tr> 
</table>
From stackoverflow
  • You could do it like this:

        $("#tblOne").find("tr").click(function() {
            var election = $(this).find("td").eq(0).html();
            var worker = $(this).find('input[name=theName]').val();
        });
    

    Read through this excellent article 'How to get what you want using jQuery' by Benjamin Sterling.

    zSysop : Thanks. I'm using a master page and Asp.net changes the id name. Is there a way to use clientID? I've tried using var worker = $(this).find('input[name=<%=hdnfld_Id.ClientID %>]').val(); and i get the following error. The name 'hdnfld_Id' does not exist in the current context.
  • <asp:HiddenField id="foo"> generates an <input type="hidden" id="foo"/> does it not? Why don't you just do

    $("#foo").val()
    

    ?

    I think you need to explain what you're trying to do a bit better. If you find that

    $(this).find(":input").val();
    

    ... only works when you have one input, maybe what you're looking for is this:

    $(this).find(":input").each(function() {
      // Prints the value of each input.
      alert($(this).val());
    }
    

    But as it stands, your question is not very clear. Try editing your question and take your time to explain exactly what you want.

    zSysop : Well i was looking for a way to access the values in the hidden fields for the click event on a row within the table. Each hidden field has a unique identifier for the row. I hope that clears it up a bit.
  • With the way you have it setup right now, you could do this:

    $('tr td', '#tblOne').eq(0).find(':input').val(); // find input in 1st TD
    $('tr td', '#tblOne').eq(1).find(':input').val(); // find input in 2nd TD
    

    Using this you don't have to worry about the input's ClientID.

  • Why don't you simply use this:

    jQuery("#<%=hdnfld_Id.ClientID%>")
    

0 comments:

Post a Comment