Asked  7 Months ago    Answers:  5   Viewed   35 times

What are the ways to get and render an input value using jQuery?

Here is one:

$(document).ready(function() {
  $("#txt_name").keyup(function() {
    alert($(this).val());
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<input type="text" id="txt_name" />

 Answers

86
//Get
var bla = $('#txt_name').val();

//Set
$('#txt_name').val(bla);
Tuesday, June 1, 2021
 
Maury
answered 7 Months ago
59

You could set its width:

<input type="text" id="text" name="text_name" style="width: 300px;" />

or even better define a class:

<input type="text" id="text" name="text_name" class="mytext" />

and in a separate CSS file apply the necessary styling:

.mytext {
    width: 300px;
}

If you want to limit the number of characters that the user can type into this textbox you could use the maxlength attribute:

<input type="text" id="text" name="text_name" class="mytext" maxlength="25" />
Sunday, August 8, 2021
 
OMGKurtNilsen
answered 4 Months ago
47

This should do it I think

$("#formId input:text, #formId textarea").first().focus();
Monday, August 9, 2021
 
Bere
answered 4 Months ago
61

zzzzBov is correct however it would be better to use a context to improve efficiency and/or a tagname. In addition add a variable that references $(this).

var $this = $(this);
$('p[name="'+$this.attr('id')+'"]', $this.closest('tr')).text('foo bar baz'); 

Edited after comment The difference between above and zzzzBov's answer is:

$('[name="'+$(this).attr('id')+'"]')  
// will start traversing the DOM starting at the beginning of the 
// document. Largest search.

$('p[name="'+$this.attr('id')+'"]')
// will first call getElementsByTagName and try to find a match, smaller search.

$('p[name="'+$this.attr('id')+'"]', $this.closest('tr'))
// will only search the contents of the closest('tr') tag 
// that is an ancestor of your input. Even smaller search.

Generally speaking being more specific with jQuery selectors can improve preformance. In addition using variable to store selected elements such as var $this = $(this); or var $myElement = $('#myElement') is more efficient than creating the same selectors over and over. Remember jQuery is as efficient as it can be with the selectors but it is up to you to use them to thier potential.

Friday, August 20, 2021
 
BDL
answered 4 Months ago
BDL
87

The problem is that when you dynamically create the controls (table, rows, cells, textbox) in the AddTxt method you are assigning an ID value to the textbox, however when you dynamically create the controls in the OnInit event, then you are not creating ID value at all, thus the ViewState cannot be wired up to what the user typed in and thus lost.

If you create the textbox with the same ID value (txtBox.ID = "txt" + i.ToString() + j.ToString();) as in the other dynamic control creation logic, then your ViewState value would be preserved and properly applied to the textbox upon it being recreated in OnInit.

Note: ViewState is heavily dependent upon the ID values of server controls in order to properly associate values of controls between full and partial postbacks.

Sunday, August 29, 2021
 
Teno
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share