0

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?

3 Answers 3

4

get value:

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

assign value:

$("#emp").val("this is a new value");
Sign up to request clarification or add additional context in comments.

Comments

3
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.

Comments

0

Better cache the element than the value:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.