.prop () VS.val (). Set input text value using jQuery

After reading (interesting) .prop () vs .attr () and jQuery Performance: attributes of doubt in my understanding about which is better to use: .prop () or .val ()? I want to set the value of the input text.

The jQuery.prop () page indicated the following:

Properties typically 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 check box. The .prop () method should be used to set the disabled and marked instead of the .attr () method. To get and set the value , use the .val () method.

But for performance, .prop () seems better than the values ​​of .val (): .prop () vs .attr () vs .val ()

So I have no answer. What should I do?

$('#element').prop('value', 'Example');

or

$('#element').val('Example');

EDIT1: .prop () is always better than .val () enter image description hereenter image description hereenter image description here

EDIT2: I tried now to get the value , also .prop () is faster enter image description here

EDIT3: @BeatAlex , , nativece js1

var x = document.getElementById('element').value;

enter image description here

, , .val() , .

PS: , jQuery - 1.11 2.1

+4
5

val(). , , val ue .

, val , .

,

val() .

, ,

.val().

, , , :

$('input').val();

, , , , val() , prop().

LOVE, , :

Pure js is better

Pure JS , .

+11

, prop val, !

val / "" node (s). , . ( ).

, val, attr ('value') prop ('value'), ?

, , . .

prop Boolean-like DOM node. attr val

:

<input type="checkbox" checked />
<input type="checkbox" checked="checked" />
<input type="checkbox" checked="false" />
<input type="checkbox" checked="true" />
  • / val()
  • , , , attr ('checked'). .
  • attr ('checked') if, , , if ('false')

- - prop.

, , attr, val

+4

, prop() , val() .

val() prop() ( Ex: ), val() , prop().

val() , , , , .

, , prop() .

+1

.

jQuery "" , val() .

IE 8 .

:

HEL


LO

.val() "HEL\n\nLO" 7

.prop("value") "HEL\r\n\r\nLO" 9

+1

, . , jQuery " " usecase, , .

  • attr() - HTML . , , . , write attr()
  • prop() - DOM .
  • val() - .
0

Source: https://habr.com/ru/post/1529394/


All Articles