How to get label value in textbox using jQuery

My question is similar to this: How to get the value of a text field in a label using jquery , but I'm trying to do the opposite: I am trying to get the value of a label in a text field. I thought it would be easy to switch elements in the code, but this is not the case, apparently. I also looked at some of the questions presented in the section โ€œQuestions that may already have your answerโ€ below my name, but did not find what helped me (maybe the solution was in one of them, but I just did not understand this .. .).

Here is my html table:

<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 above post noted, this does not work:

 $('input#hdnBranchName').val() = $('label#lblBranchName').text(); 

I tried these:

 $('input#txtBranchName').html($('label#lblBranchName').val()); $('input#txtBranchName').text($('label#lblBranchName').val()); 

None of them worked. So I tried to check if I could not select the text box correctly:

 $('table#tblBranchDetails input#txtBranchName').html($('label#lblBranchName').val()); $('table#tblBranchDetails input#txtBranchName').text($('label#lblBranchName').val()); 

But not one of them worked.

How to do it and how to find out more: why I donโ€™t think that an explicit method works?

Thanks!

0
jquery label textbox
source share
5 answers

take a look at the docs ..

A line of text or an array of lines corresponding to the value of each matched item to set as selected / checked. This method is usually used to set the values โ€‹โ€‹of form fields.

try it

 $('input#hdnBranchName').val($('label#lblBranchName').text()); 

val() is to get the selected input value ... where it should be set as val('') .. this sets an empty value to the selected input field

+3
source share
 $('input#txtBranchName').val($('label#lblBranchName').text()); 

If you want to change the value of input , you must use val . text can extract the text content of an html element (so it works fine with label ).

+2
source share

Try the following:

 $('input#txtBranchName').attr( 'value',$('label#lblBranchName').val() ); 
+1
source share

They will work if you have text in the shortcut

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

or

 $('#txtBranchName').val($('#lblBranchName').html()); 
+1
source share

try the following:

 $('#txtBranchName').val($('#lblBranchName').text()); 
0
source share

All Articles