0

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!

Community
  • 1
  • 1
marky
  • 4,878
  • 17
  • 59
  • 103

5 Answers5

3

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

bipen
  • 36,319
  • 9
  • 49
  • 62
  • I tried all of the suggestions and the only ones that worked were "$('input#hdnBranchName').val($('label#lblBranchName').text());" and "$('input#hdnBranchName').val($('label#lblBranchName').html());" Thanks for the explanation, @bipen – marky Jan 24 '13 at 13:22
2
$('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).

Magus
  • 14,796
  • 3
  • 36
  • 51
1

Try this:

$('input#txtBranchName').attr( 'value',$('label#lblBranchName').val() );
Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52
1

These will work, provided you have some text in label

$('#txtBranchName').val($('#lblBranchName').text());

or

$('#txtBranchName').val($('#lblBranchName').html());
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

try this :

$('#txtBranchName').val($('#lblBranchName').text());
guri
  • 662
  • 2
  • 8
  • 26