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() {
<script src=""></script>
<input type="text" id="txt_name" />


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

Tuesday, June 1, 2021
answered 7 Months ago

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
answered 4 Months ago

This should do it I think

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

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:

// will start traversing the DOM starting at the beginning of the 
// document. Largest search.

// 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
answered 4 Months ago

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
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 :