Reputation: 5068
My question is similar to this: How to get textbox value into label using jquery, but I'm trying to do opposite: I'm trying to get the value of a label into a into a textbox. I thought it'd be a simple matter of switching the elements in the code, but that's not the case, apparently. I also did take a look at some of the questions presented in the "Questions that may already have your answer" section below my title, but didn't find something that helped me (maybe the solution was in one of them, but I just didn't understand it...).
Here's my table's html:
<table id="tblBranchDetails">
<tr>
<td width="120px">Branch:</td>
<td id="branchName" class="branchData">
<label id="lblBranchName"></label>
<input type="text" id="txtBranchName" />
</td>...
As the author of the post above noted, this doesn't work:
$('input#hdnBranchName').val() = $('label#lblBranchName').text();
I've tried these:
$('input#txtBranchName').html($('label#lblBranchName').val());
$('input#txtBranchName').text($('label#lblBranchName').val());
Neither of those worked. So I tried to see if maybe I wasn't selecting the textbox correctly:
$('table#tblBranchDetails input#txtBranchName').html($('label#lblBranchName').val());
$('table#tblBranchDetails input#txtBranchName').text($('label#lblBranchName').val());
But neither of those worked either.
How do I do this, and as a matter of learning more: why doesn't what I would assume to be the obvious method work?
Thanks!
Upvotes: 0
Views: 3939
Reputation: 133403
These will work, provided you have some text in label
$('#txtBranchName').val($('#lblBranchName').text());
or
$('#txtBranchName').val($('#lblBranchName').html());
Upvotes: 1
Reputation: 31829
Try this:
$('input#txtBranchName').attr( 'value',$('label#lblBranchName').val() );
Upvotes: 1
Reputation: 36531
have a look to the docs..
A string of text or an array of strings corresponding to the value of each matched element to set as selected/checked. This method is typically used to set the values of form fields.
try this
$('input#hdnBranchName').val($('label#lblBranchName').text());
val()
is to get the selected input value... where as val('')
is to set.. this sets empty to the selected input field
Upvotes: 3
Reputation: 15104
$('input#txtBranchName').val($('label#lblBranchName').text());
If you want to modify an input
value, you must use val
. text
can retrieve the text content of an html element (so it's works perfectly with a label
).
Upvotes: 2