user3858417
user3858417

Reputation:

jquery giving me undefined value for element

I have the following html

$('#div1').append("<table id='" + response + "_" + "fawad" + "' width='100%' style='border-bottom-style:inset; border-bottom-width:thin'><tr><td width='595px'>Company Name: </td><td id='" + response + "_" + "fawad" + "_cname'>" + txtName.value + "</td></tr><tr><td>From Date </td><td id='" + response + "_" + "fawad" + "_fromd'>" + txtFrom.value + "</td></tr><tr><td>To Date </td><td id='" + response + "_" + "fawad" + "_tdate'>" + txtTo.value + "</td></tr><tr><td>Position </td><td id='" + response + "_" + "fawad" + "_position'>" + txtPosition.value + "</td><td><linkbutton  onclick='passp(this)'>edit</linkbutton></td></tr></table>");

I using Jquery to access the element value like this

$("#txtName").val($($(elem).closest('table').attr('id')+"_cname").val());

it's giving me undefined in txtName value.

txtName is a textbox

<asp:TextBox ID="txtName" runat="server"> </asp:TextBox>

Please help

Upvotes: 0

Views: 298

Answers (2)

Jay Blanchard
Jay Blanchard

Reputation: 34426

You're trying to get the value (val()) which is only applicable for input elements. Change to html() -

$("#txtName").val($($(elem).closest('table').attr('id')+"_cname").html());

If #txtName is not an input you will need to change the method there too. plus it appears that there are some typos. See this example: http://jsfiddle.net/2axfN/

$("#txtName").html( $('td:first').closest('table').attr('id') );

Updated with an input field: http://jsfiddle.net/2axfN/1/

$('input[name="foo"]').val( $('td:first').closest('table').attr('id') );

One more update to add '_cname' to the end of the value:

$("#txtName").html( $('linkbutton').closest('table').attr('id') + "_cname");

http://jsfiddle.net/2axfN/3/

Upvotes: 1

eduardobr
eduardobr

Reputation: 145

$("#txtName").val($('#' + $(elem).closest('table').attr('id')+"_cname").text());

Should solve it.

You were trying to get an element by id without "#", and trying to get val() of a td.

I put the # and changed val() to text().

Upvotes: 1

Related Questions