After reading (the interesting) .prop() vs .attr() and jQuery Performance : attributes doubt arise in my mind about what is better to use: .prop() or .val() ? I want to set an input text value.
The jQuery .prop() page said the following:
Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val() method should be used for getting and setting value.
But regarding to performance .prop() seems to be better than .val() setting values:
So I don't have an answer. What I should do?
$('#element').prop('value', 'Example');
or
$('#element').val('Example');
EDIT1:
.prop() is always better than .val()
EDIT2:
I've tried now getting a value, also .prop() is faster
EDIT3: As @BeatAlex says if we want performace native js is extremely faster
var x = document.getElementById('element').value;
As some of you said it may be faster, but if .val() exist we'd use it.
PS: while I am writing this, current jQuery version is 1.11 or 2.1