tags:

views:

54

answers:

3

Hi,

In javascript I can use the following code both ways, i.e:

var a = document.getElementById("emp").value;
document.getElementById("emp").value = a;

So I can basically retrieve a value as well as assign a value using ...().value

In jQuery, what is the equivalent to the above, i.e retrieve and assign

Thanks. Tony.

+4  A: 

get value:

var empVal = $("#emp").val();

assign value:

$("#emp").val("this is a new value");
Marek Karbarz
+3  A: 
var a = $('#emp').val();
$('#emp').val(a);

The val accessor works like the value property, except that it's extended to work nicely even with input fields like selects and checkboxes. See its documentation.

hobbs
A: 

Better cache the element than the value:

var $a = $("#emp");
$a.val($a.val());
Gumbo